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)
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