Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why does the value not rise by 1 every time I click on a particular part?

Asked by 5 years ago

I have recently been making a game where I need to make the leaderstats to grow by 1 when a player clicks on a particular part. I have tried hard to make it work but I can't solve the problem. Can you help me? I will remember to accept a fulfilling answer

game.Players.PlayerAdded:connect(function(player)

 local StartingMoney = 0
 local leaderstats = Instance.new("Folder")
 leaderstats.Name = "leaderstats"
 leaderstats.Parent = player

 local money = Instance.new("NumberValue")
 money.Name = "Money"
 money.Parent = leaderstats
 money.Value = StartingMoney
    game.Workspace.Part.ClickDetector.MouseClick:Connect(function()
    local pstats = game.Players[player.Name].leaderstats
    pstats.Value = money + 1
    end)
end)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Your problem is here:

    local pstats = game.Players[player.Name].leaderstats
    pstats.Value = money + 1

pstats is the Folder you made called "leaderstats", not the money value object. Additionally, you're trying to add 1 to "money" which is an Instance reference.

Because of how Lua upvalues work, you can just use the money reference you already have in your closure, by replacing the two lines above with:

money.Value = money.Value + 1
Ad

Answer this question