Leaderstats not showing for other players

I don’t know a whole lot about scripting, so this is very confusing.

I’m working on a simulator game and I am using a script I found for leaderstats. While testing with a friend, I noticed that we couldn’t see each others balance. I went into Team Test to notice the same thing.

Here’s the leaderstats script:

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"

	local Amount = Instance.new("IntValue",leaderstats)
	Amount.Name = "Money"
	Amount.Value = 0
end

game.Players.PlayerAdded:Connect(onPlayerJoin) 

And here is the script that adds points to the leaderboard:

local leaderstats = game.Players.LocalPlayer.leaderstats
local Money = leaderstats:WaitForChild("Money")

script.Parent.Activated:Connect(function()
	Money.Value = Money.Value + 1
	script.Disabled = true
	wait(3.1)
	script.Disabled = false
end)

If you have an answer then please let me know! Thank you.

3 Likes

It is because you are adding money on a local script. You must change the money value on the server.

You can fire a remote event from the client to do this

Your local script:

script.Parent.Activated:Connect(function()
	YourRemoteEvent:FireServer()
end)

The server script:

YourRemoteEvent.OnServerEvent:Connect(function(player)
	player.leaderstats.Money.Value += 1 --this is the same thing as money.value = money.value + 1
end)

By the way, put the RemoteEvent somewhere in ReplicatedStorage

4 Likes

Didn’t work and I don’t know why

I added the script into the tool that fires the remote event and it didn’t work. I also tried changing the script to the following:

script.Parent.Activated:Connect(function()
     game.ReplicatedStorage.IncreaseMoney.FireServer()  --- "IncreaseMoney" is the remote event
end)

Don’t know if there is any other way to fix this though.

1 Like

You have to use a colon after IncreaseMoney because it is a built in function.

game.ReplicatedStorage.IncreaseMoney:FireServer()

Also, add some print statements inside of the tool script and the server script to make sure that it is firing.

1 Like

This bug happens because client changes don’t replicate to the server, therefore only you can see the change and no one else can.

To fix this, change the LocalScript to a Script and reference the player using :GetPlayerFromCharacter instead of LocalPlayer. You also should be using variables for a debounce instead of script.Disabled.

RemoteEvents overcomplicate it and will introduce a whole new world of vulnerabilities @PolyLacticSugarcane.

Code:

local Players = game:GetService("Players")

local tool = script.Parent
local debounce = false

tool.Activated:Connect(function()
	if debounce then
		return
	end
	
	local player = Players:GetPlayerFromCharacter(tool.Parent)
	local leaderstats = player and player:FindFirstChild("leaderstats")
	local Money = leaderstats and leaderstats:FindFirstChild("Money")
	
	if not Money then
		return
	end
	
	debounce = true
	
	Money.Value += 1
	
	task.wait(3.1)
	debounce = false
end)
2 Likes

You could also put a script in the tool. And then add Money with a Tool.Activated Event.

1 Like

Make a remote event

reason why this aint working is because the money increased the value in only client side (only the player that is playing is seeing) and not server side (the whole server).

1- make a remote event in the ReplicatedStorage and name it “AddMoney”

2- make a script in the ServerScriptService:

game.ReplicatedStorage.AddMoney.OnServerEvent:Connect(function(player)
   player.leaderstats.Money.Value += 1
end)

3- fire the remote event in the same script you made

local leaderstats = game.Players.LocalPlayer.leaderstats
local Money = leaderstats:WaitForChild("Money")

script.Parent.Activated:Connect(function()
	game.ReplicatedStorage.AddMoney:FireServer()
	script.Disabled = true
	wait(3.1)
	script.Disabled = false
end)
2 Likes

I understand your concern about leaderstats not syncing between players in your simulator game. It looks like you’ve made some progress, and let me explain what you’ve done and should have:

1. Leaderstats Setup Script

You have a script that runs when a player joins the game. This script creates a “leaderstats” folder for each player, along with an “IntValue” named “Money” inside it. This is the basic setup for tracking player money.

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local Amount = Instance.new("IntValue", leaderstats)
	Amount.Name = "Money"
	Amount.Value = 0
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

Path: game.ServerScriptService.leaderboardScript

2. Client-Side Script

You have another script on the client side, which is likely attached to a GUI button. This script increases the player’s money when the button is clicked. It uses a Remote Event to communicate with the server and increment the “Money” value.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.Remotes.AddMoney

script.Parent.MouseButton1Down:Connect(function()
	remoteEvent:FireServer()
	-- No need to check for success on the client side
	script.Disabled = true
	wait(3.1)
	script.Disabled = false
end)

Path: game.StarterGui.ScreenGui.Frame.TextButton.LocalScript

3. Server-Side Script

On the server side, you have a script in ServerScriptService that listens for the Remote Event and increments the player’s money securely using a server function. This script responds to the client’s request to add money to the player’s leaderstats.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function addMoney(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		local money = leaderstats:FindFirstChild("Money")
		if money then
			money.Value = money.Value + 1
			return true -- Successfully added money
		end
	end
	return false -- Failed to add money
end

local remoteEvent = ReplicatedStorage.Remotes.AddMoney
remoteEvent.OnServerEvent:Connect(addMoney)

Path: game.ServerScriptService.Script

ReplicatedStorage: have a Remote Event named “AddMoney” in ReplicatedStorage. This is used to communicate between the client and server to ensure that the money increase happens securely on the server side.

Here’s how it works:

  • When a player clicks the GUI button, (Using GUI Button as example) the client-side script triggers the “AddMoney” Remote Event and requests an increase in money.

  • The server-side script in ServerScriptService listens for the Remote Event and handles the request securely, ensuring that the money increase happens on the server.

  • If the money is increased successfully, the server-side script returns “true,” and the client script can disable the button temporarily. The wait(3.1) provides a delay before re-enabling the button.

Your setup appears correct, and this design should ensure that the player’s money is accurately updated across all clients, making your simulator game more enjoyable for everyone. If you encounter any issues or have further questions, please feel free to ask for assistance. Good luck with your game development!
:stuck_out_tongue_winking_eye: :+1: