Values aren't changing for coins and speed in leaderboard script

So I am trying to change Speed value , Coin value and Character’s speed. But they don’t change. I guess mistake is where I put it. I put it in workspace

a = 0
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Wait()

local leaderstats = Instance.new("Folder",Player)
leaderstats.Name = "leaderstats"

local Speed = Instance.new("IntValue",leaderstats)
Speed.Name = "Speed"
Speed.Value = Player.Character:WaitForChild("Humanoid").WalkSpeed

local Coins = Instance.new("IntValue",leaderstats)
Coins.Value = 0
Coins.Name = "Coins"

Player.Character.Humanoid.Died:Connect(function()

     Player.Character.Humanoid.WalkSpeed = a+16 
    Coins.Value = a

Speed.Value = a+16

workspace.Part.Touched:Connect(function()
a = a + 1

end)
    end)

end)

1 Like

First of all move the a varible into your function

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Wait()
    local a = 0

    local leaderstats = Instance.new("Folder",Player)
    leaderstats.Name = "leaderstats"

    local Speed = Instance.new("IntValue",leaderstats)
    Speed.Name = "Speed"
    Speed.Value = Player.Character:WaitForChild("Humanoid").WalkSpeed

    local Coins = Instance.new("IntValue",leaderstats)
    Coins.Value = 0
    Coins.Name = "Coins"

    Player.Character.Humanoid.Died:Connect(function()

            Player.Character.Humanoid.WalkSpeed = a+16
            Coins.Value = a
            Speed.Value = a+16

            workspace.Part.Touched:Connect(function()
                a = a + 1
            end)
    end)
end)

As if you don’t your sharing that variable with all players.

That doesn’t help much also why does it matter to be inside?

Since you don’t want to share speed/coins with all players. Also, what is not changing [both or just speed/coins]. Any errors?

Try using a localscript if you don’t use (It usually works when I do)

You can’t use a localscript to create a leaderboard or to change stats. If you change stats through a local script the server won’t be able to see the changes and it is easily hackable.