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

Add a value overtime?

I want to add 2 every second to a value called "MoneyValue".

lvl1money = 2 -- the amount of money you get per second
game.Players.PlayerAdded:connect(function(player)
for i,v in pairs(game.Workspace.Mines:GetChildren()) do
	local money = player.leaderstats.Money.Value
    v.Name = math.random(1,100) --random names for all the goldmines
	wait(1)
	v.Collect.MoneyValue.Value = v.Collect.MoneyValue.Value + lvl1money -- adds money overtime to value in mine (collect)
	print(v.Collect.MoneyValue.Value)
end 
end)

This is a script in ServerScriptService.

1 answer

✓ Accepted answer

This script is not a loop, it only runs when the player joins. That is your issue, also, try to use ipairs, its more object based, its optional tho. I have tried using a while true do inside of a playeradded function and seems to not work by the way, I think repeat may be a good way to use it.

lvl1money = 2 -- the amount of money you get per second
game.Players.PlayerAdded:connect(function(player)
repeat
for i,v in pairs(game.Workspace.Mines:GetChildren()) do
	local money = player.leaderstats.Money.Value
	 v.Name = math.random(1,100) --random names for all the goldmines
	 wait(1)
	 v.Collect.MoneyValue.Value = v.Collect.MoneyValue.Value + lvl1money -- adds money overtime to value in mine (collect)
	print(v.Collect.MoneyValue.Value)
end
end)

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