Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Leaderstats Not Updating Properly? How To Fix?

So I am trying to make it so when my player clicks they get a coin. But the leaderstats are not updating.

This is my local script

game.Players:WaitForChild(game.Players.LocalPlayer, 20)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local coins = player.leaderstats.Coins.Value

mouse.Button1Down:Connect(function()
	coins = coins + 1
	print(coins)
end)

1 comment

1

Why is there a , 20 in the first line?

2 answers

The reason why this isn't working is because you can't edit values in a local script, the solution to this would be to use remote events to fire a server script, which would require you to put a RemoteEvent in ReplicatedStorage. From there, you would need to edit your local script to look like this:

RS = game:GetService("ReplicatedStorage")
RE = RS:WaitForChild("RemoteEvent")
game.Players:WaitForChild(game.Players.LocalPlayer, 20)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
    RE:FireServer()
end)

Then, you would have to create a new script, which I would recommend to put in ServerScriptService, that looks like this.

RS = game:GetService("ReplicatedStorage")
RE = RS.RemoteEvent

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		RE.OnServerEvent:Connect(function(lPlayer)

			if lPlayer == Player then

				Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value + 1
				print(Player.leaderstats.Coins.Value)
			end

		end)
	end)
end)

Please let me know if you need any help!

Edit: For some people in the comments, it's true that it can change locally, but I made it public as it was leaderstats. I also fixed the script where I believe it will now work.

3 comments

0

You are wrong, you can edit leaders stats from localscript but the changes will only show for that player, it doesnt replicate, the reason it doesnt change .Value is immutable, https://gyazo.com/2d8c50f3976c26b05644b00135be118d see this 

0

Why do you need the CharacterAdded and PlayerAdded? Just do the events on a separate script.

0

And when i change the variable to .Value, https://gyazo.com/b3488e971ed4afeeaea076482a9745fb as you can see in the output log the print does print the correct value but the leaderstats arent updating

You are updating a number and not a value.

game.Players:WaitForChild(game.Players.LocalPlayer, 20)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local coins = player.leaderstats.Coins.Value

mouse.Button1Down:Connect(function()
    player.leaderstats.Coins.Value = coins + 1
    print(player.leaderstats.Coins.Value)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.