How can I fix this?

Ok, so if you have seen me before on the forums, you know I have almost no knowlegde of Luau. So I tried to make something where the player joins and they get 5 “Bucks” every 60 seconds. I know this doesn’t work but how can I make it work?

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

  local Bucks = Instance.new("IntValue")
Bucks.Name = ("Bucks")
Bucks.Parent = leaderstats

  local Time = Instance.new("IntValue") -- ignore this its just for something else
Time.Name = ("Time in server")
Time.Parent = leaderstats

-- how would i make it so that when the player joins, this would apply to them
while true do
Bucks.Value = Bucks.Value + 5
wait(60)
    end
end

Here some code I made to help you out:

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

  local Bucks = Instance.new("IntValue")
Bucks.Name = ("Bucks")
Bucks.Parent = leaderstats

  local Time = Instance.new("IntValue") -- ignore this its just for something else
Time.Name = ("Time in server")
Time.Parent = leaderstats

-- how would i make it so that when the player joins, this would apply to them
while wait(60) do
player.leaderstats.Bucks.Value = Bucks.Value + 5
end
end)
1 Like

That should help you, if it does please mark it as the solution, if it doesn’t please tell me.

Alright I will mark it as the solution, and also thank you for the help.

No problem, I’m glad I could help you! :smile:

1 Like

Only thing I’d change (besides indentation) is
player.leaderstats.Bucks.Value = Bucks.Value + 5
to
Bucks.Value += 5.

1 Like

Lua doesn’t have increment/decrement shortcuts like that, last I checked.

ROBLOX doesn’t use Lua. They use Luau which has modifications which are not really documented anywhere. You have to google “ROBLOX compound operators” to find out about this feature.

https://developer.roblox.com/en-us/resources/release-note/Release-Notes-for-435

Luau now supports compound assignments ( += , -= , *= , /= , %= , ^= and ..= ). The assignments only work on single values ( a, b += 1 is invalid); the left hand side is only evaluated once. When modifying table keys, __index and __newindex will be called as necessary - no new metamethods are introduced.

2 Likes

But Luau does.
It supports +=,-=,*= and /= (and %=,^= which I wasn’t sure about)

1 Like