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

While true do leaderboards not giving money each 5 seconds [beginner]?

Hi, so I am learning to script and I was playing around with leaderboards. I do not know a lot about scripting, but what I want to do is: Every second I will get 1 point into "Days", which works fine, but after 5 seconds it will reset. This also works fine the timer always resets at 5 and goes from the beginning, but what I am trying to do is for each 5 days it passes I would like to get + 10 "Cash" but it is not giving it, why is that?

game.Players.PlayerAdded:Connect(function(player)
	
local stats = Instance.new("IntValue", player)
stats.Name = "leaderstats"
	
local points = Instance.new("IntValue", stats)
points.Name = "Days"

local money = Instance.new("IntValue", stats)
money.Name = "Cash"


while true do
	
	
	points.Value = points.Value + 1
	wait(1)
	if points.Value >= 5 then
		points.Value = 0 and money.Value == money.Value + 10
		
	end
end	
		
end)

Thank you!

4 comments

2

You dont need 2 equals (==) when your not using an if statement simply remove the 2 equals

0

any errors?

0

Right

0

Thank you, but now I have another glitch what happens is that it goes to day 5 and then it skips to 11 and nothing goes up anymore.

2 answers

✓ Accepted answer

You'd need coroutine function. Try this:

game:GetService("Players").PlayerAdded:Connect(function(player)

	local stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"
	
	local points = Instance.new("IntValue", stats)
	points.Name = "Days"
	
	local money = Instance.new("IntValue", stats)
	money.Name = "Cash"
	
	
	coroutine.wrap(function()
		while true do
			player.leaderstats.Days.Value = player.leaderstats.Days.Value + 1
			 wait(1)
		    	if player.leaderstats.Days.Value >= 5 then
				player.leaderstats.Days.Value = 0 
				player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 10
			end
		end 
	end)()
end)

Corountines are used to do something without yielding the script. Anything inside of a coroutine's scope is basically another script. For more information go here.

2 comments

0

Thanks to everyone! It works I am sure going to look into the coroutine function, thank you!

0

Also, don't use the second argument on Instance.new() since it's inefficient. Instead, set the parent using "stats.Parent = player, points.Parent = stats, etc."

This should work:

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"
	
	local points = Instance.new("IntValue", stats)
	points.Name = "Days"
	
	local money = Instance.new("IntValue", stats)
	money.Name = "Cash"

	while wait(1) do
		points.Value = points.Value + 1
		if points.Value >= 5 then
	        	points.Value = 0
			money.Value = money.Value + 10 -- You do not use 'and' to set multiple values.
		end
	end 
end)

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