So I basically want to create a simulator with a per second system, but it doesn’t work. I tried a few times modifying the code but it still doesn’t work. Below is my script. Any help appreciated!
game.Players.PlayerAdded:Connect(function(player)
while wait(1) do
player:WaitForChild("leaderstats"):WaitForChild("Coins").Value += player:WaitForChild("PerSecond").Value
end
end)
It seems you didn’t assign any value for the IntValue PerSecond.
Also, try having only one PlayerAdded event, you can move the while loop inside the first PlayerAdded event, after you assign the values.
Like this:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local multiplier = Instance.new("IntValue")
multiplier.Name = "Multiplier"
multiplier.Parent = player
local perSecond = Instance.new("IntValue")
perSecond.Name = "PerSecond"
perSecond.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
while wait(1) do
coins.Value += perSecond.Value
end
end)
Edit: Just updated the code based on @HafuPlay’s inquiry.
I think it is better to take the while loop out of the playeradded event and (optional) spawn it. Then instead of referencing just that one player you can loop through every player and increment their per second value by one and add it to the coins value. This is more optimised as it prevents the number of while loops running at that moment.