how to increase leaderboard number on click event

31 Views Asked by At

so i have a script that should make a number on a leaderboard go up when an object is clicked, but 1, it doesnt work. 2, i want it to be a normal click event instead of clicking a certain object.

the code in serverscriptservice:

local Players = game:GetService("Players")

local function leaderboardSetup(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local gold = Instance.new("IntValue")
    gold.Name = "Gold"
    gold.Value = 0
    gold.Parent = leaderstats

    local function upd()
        gold.Value = gold.Value + 1
    end
    game.ReplicatedStorage.GoldClick.OnServerEvent:Connect(function(Localplayer)
        if player.Name == Localplayer.Name then
            upd()
        end
    end)
end

Players.PlayerAdded:Connect(leaderboardSetup)

the code in the object:

script.Parent.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.GoldClick:FireServer()
end)

i tried to make the leaderboard go up with the leaderstat name being "Gold", there were no errors but when i clicked the part it didn't go up.

1

There are 1 best solutions below

0
MobiDev On

There is multiple problems with this, Firstly, you are listening to the same RemoteEvent multiple times, this is going to cause problems since you can only handle a RemoteEvent once.

You should move this event outside of the setup function and have a function to update the statistic for any inputted player.

Secondly, assuming that the script inside the Part is a LocalScript, it will not run by default when under workspace. You could instead use a serverside Script to listen to the event (could be the same script that's setting up the leaderstats, move the LocalScript somewhere where it will execute (such as StarterPlayerScripts) or set the RunContext of the LocalScript to "Client". see (https://devforum.roblox.com/t/live-script-runcontext/1938784)