While true do loop speeds up

I have this while true do loop which is supposed to add 1 to the seconds’ value per second but I made so it does that every 3 seconds. This is because after a player plays for a longer time the loop starts speeding up and the value goes up in like 5 per second. How do I make it so it stays at 1 per second.

while wait(3) do
				
				local g = true
				if g then
				Player.leaderstats.Seconds.Value += 1
			if ga == true then
				break
					end
				g = false	
				end

Can you tell us what is the variable « ga »?

1 Like

This part is in a server script which saves data and has a deaths and checkpoints value. So the data saves when the player leaves ,the loop breaks.

I am not really sure about what it is but you made a small mistake at the line « Player.leaderstats.Seconds.Value += 1». Excepted this little thing, i will let someone else answer i think :man_shrugging:

1 Like

The seconds work, it is just that for some reason it starts speeding up the loop if someone plays for a long time.

I mean you did +=1, isn’t it supposed to be = +1?

1 Like

I will try to do that right now.

I have that with my deaths value as well and it works fine. I do not think that is the problem.

+= is an assignment operator. x += y is equivalent to x = x + y.

3 Likes

So I am correct, right posatta.

1 Like

Welp i will let someone else answer because i do not have the answer :man_shrugging: Sorry

1 Like

Is anyone going to answer my question, I have had so many problems with this script for like 2 weeks. This is the last problem. Plz help someone :sob:

If I got it right, you want to increase the speed of the loop?

1 Like

No, i want it to stay at 1 per second but for some reason if the player plays for longer time, the seconds increase to like 5 per second. This is why I put it at like 1 per 3 seconds.

I’m not entirely sure, but does this work?

while true do
    wait(1)
    -- stuff
end

Maybe the while wait(3) do part is messing things up. I’ve heard that you should avoid wait at all costs. If this doesn’t work, a Heartbeat function should work (hopefully).

1 Like

I will try your new script right now. It will take like 10 minutes.

It is getting faster and faster. Look:


Sorry for the quality.

Can’t you just do this? :

while true do
    wait(1)
    Player.leaderstats.Seconds.Value += 1
end
1 Like

I have tried that but it did not work. Here is the full script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")
local RunService = game:GetService("RunService")
local checkpoints = workspace.Checkpoints
local ga = false
Players.PlayerAdded:Connect(function(Player)
	local PlayerDataStore = DataStoreService:GetDataStore("PlayerStore", Player.UserId)
	warn("Initializing leaderstats...")
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = Player
	local wipeouts = Instance.new("IntValue")
	wipeouts.Name = "Deaths"
	wipeouts.Parent = stats
	local second = Instance.new("IntValue")
	second.Name = "Seconds"
	second.Parent = stats
	 local stage = Instance.new("IntValue")
    stage.Name = "Stage"
	stage.Parent = stats
	print("Completed.")
	
	warn("Initializing leaderstats values...")
	local success = PlayerDataStore:GetAsync("success")
	local PlayerData = PlayerDataStore:GetAsync("PlayerData")
	if success == nil or false then
		print("Player is currently nil")
		PlayerDataStore:SetAsync("success", true)
		PlayerDataStore:SetAsync("PlayerData", HttpService:JSONEncode({deaths = 0, sec = 0, stages = 0}))
		Player:Kick("Rejoin, you have no data")
	elseif success == true then
		local decoded = HttpService:JSONDecode(PlayerData)
--		for k,v in pairs(decoded) do
--			print(k, v, type(v))
--		end
		Player.leaderstats.Deaths.Value = decoded.deaths
		Player.leaderstats.Seconds.Value = decoded.sec
		Player.leaderstats.Stage.Value = decoded.stages
		print(success)
		print("Completed.")
		warn("Continuing with normal RunTime")
		Player.CharacterAdded:connect(function(Character)
			local humpart = Character.HumanoidRootPart
			RunService.Stepped:wait()
        humpart.CFrame = checkpoints[stage.Value].CFrame+ Vector3.new(0,math.rad(3.5),0)
			local d = true
			Character:WaitForChild("Humanoid").Died:Connect(function()
			RunService.Stepped:wait()
        humpart.CFrame = checkpoints[stage.Value].CFrame + Vector3.new(0,math.rad(3.5),0)
				if d then
					Player.leaderstats.Deaths.Value += 1
				end
			end)

			while wait(3) do
				local g = true
				if g then
				Player.leaderstats.Seconds.Value += 1
			if ga == true then
				break
					end
				g = false	
				end
			
		end
		
		end)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	ga = true
	local PlayerDataStore = DataStoreService:GetDataStore("PlayerStore", Player.UserId)
	warn(string.format("%s IN QUEUE...", Player.Name:upper()))
	local death = Player.leaderstats.Deaths.Value
	local secs = Player.leaderstats.Seconds.Value
	local stagess = Player.leaderstats.Stage.Value
	local data = {deaths = death,sec = secs, stages =stagess }
	local encoded = HttpService:JSONEncode(data)
	PlayerDataStore:SetAsync("PlayerData", encoded)
	print("Done")
end)

Someone plz help, this script has been a problem for two weeks, plz help