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

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

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

✓ Accepted answer

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

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