I have a working leaderboard with some problems. When I use a button in the starterGUI to give you 20 “jumpies”, the leaderboard updates to 20, but when I use code I found in the In-Experience Leaderboard page of the creator hub thats meant to be placed in the worldspace that gives you 10 “jumpies” for collecting a coin AFTER collecting the 20 jumpies from the button in the GUI, the counter changes to 10 instead of 30. Im getting some sort of ghost coin. Is the leaderstats not supposed to be edited from the client or something like that? If not that, i have no idea what causes this but the code should be below
Code for the Leaderstats
local Players = game:GetService("Players") --I added this part as a desperate measure, but it worked fine without it
game.Players.PlayerAdded : Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local jumpies = Instance.new("IntValue")
jumpies.Name = "Jumpies"
jumpies.Value = 7
jumpies.Parent = leaderstats
end)
Code for the GUI button (its in a script in StarterGUI, ScreenGUI,text Button.
local localPlayer = game:WaitForChild("Players").LocalPlayer
local leaderstats = localPlayer:WaitForChild("leaderstats")
local jumpies = leaderstats:FindFirstChild("Jumpies")
local button = script.Parent
local bindableEvent = localPlayer:WaitForChild("UpdateEvent")
button.MouseButton1Down:Connect(function()
jumpies.Value = jumpies.Value + 20
print("pressed")
bindableEvent:Fire()
print("fired Up")
end)
And the code for the coin in a script in an object in the workspace
local Players = game:GetService("Players")
local goldChunk = script.Parent
local function onPartTouch(otherPart)
local partParent = otherPart.Parent
local player = Players:GetPlayerFromCharacter(partParent)
local leaderstats = player and player:FindFirstChild("leaderstats")
local goldStat = leaderstats and leaderstats:FindFirstChild("Jumpies")
if goldStat then
-- Destroy the pickup
goldChunk:Destroy()
-- Update the player's leaderboard stat
goldStat.Value = goldStat.Value + 10
end
end
goldChunk.Touched:Connect(onPartTouch)
You are certainly right about the leaderstats updating through the client. You must always update it through the server, the client can only see it’s own changes, not the server or other players’ client.
Make a RemoteEvent inside of ReplicatedStorage, name it “UpdateEvent”
Inside of ServerScriptService, make a server Script and in the editor do:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.UpdateEvent -- Change the name to match how you named it
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local jumpies = Instance.new("IntValue")
jumpies.Name = "Jumpies"
jumpies.Value = 7
jumpies = leaderstats
end
local function onServerEvent(player)
player.leaderstats.Jumpies += 20
end
Players.PlayerAdded:Connect(onPlayerAdded)
remoteEvent.OnServerEvent(onServerEvent)
In a LocalScript inside of your TextButton, write:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local button = script.Parent
local remoteEvent = ReplicatedStorage:WaitForChild("UpdateEvent") -- Change the name to match how you named it
local function onActivated()
remoteEvent:FireServer()
end
button.Activated(onActivated)
In a server Script parented to the coin, write:
local Players = game:GetService("Players")
local goldChunk = script.Parent
local function onTouched(otherPart)
local player = Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
player.leaderstats.Jumpies += 10
if goldChunk.Parent then goldChunk:Destroy() end
end
end
goldChunk.Touched:Connect(onTouched)
Do remember to delete your old scripts (the ones that were causing you trouble) else they will cause conflicts and unwanted behavior
Thanks for the help! This also means that any object that updates leaderboards through the workspace also work right since the workspace is on the server side?
If the script inside of the object in workspace is a server Script with its RunContext set to Legacy (which they luckily are by default) then the leaderboard will be shown with the updated value for all players
LocalScripts don’t even work if you try using them in objects in workspace, so you don’t need to worry about them and you won’t have problems with the value only updating for 1 player