Simulator Per Second

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)

And if you want my whole script, here it is. Yes, it is a leaderstats script.
I do not see any errors in my output.

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

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.

1 Like

Why you have WaitForChild for values that you already stored in variable

I just meant to show him an example, therefore I just moved his code into the first PlayerAdded event. He can change the variables if he’d like.

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.

It doesn’t work. I didn’t get any coins per second when I changed the value to 1.

It should be like this. Make sure to spawn the while loop at the top of the script.

spawn(function()
  while wait(1) do
       for _, player in pairs(game.Players:GetPlayers()) do                          

         player.leaderstats.Coins.Value += 1
          
       end
  end
end 

NEVERMIND. I FORGOT TO SET THE VALUE TO 1 IN THE CODE.
Doing it in the explorer doesn’t work.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.