Did Roblox change anything related to while true loops?

I’ve been having issues when using while true loops, it continuously says my script is getting timed out, and Im not sure if its because of an update or not

Error:

  19:36:28.318  Script timeout: exhausted allowed execution time  -  Server - Spawner:25
  19:36:28.318  Stack Begin  -  Studio
  19:36:28.318  Script 'Workspace.BallSpawner.Spawner', Line 25  -  Studio - Spawner:25
  19:36:28.318  Stack End  -  Studio

Script:

RunService.Heartbeat:Connect(function()
	while true do
		if not Debounce then
			Debounce = true

			local BallClone = Ball:Clone() 
			BallClone.CFrame = Parent.CFrame
			BallClone.Parent = workspace

			local V = BallClone:WaitForChild("LinearVelocity")

			task.wait(1)
			V.Enabled = true

			local X = math.random(-1, 1) * 15

			if X == 0 then
				V.VectorVelocity = Vector3.new(X, 0, math.random(-1, 1) * 15)
			else
				V.VectorVelocity = Vector3.new(X, 0, 0)
			end

			task.wait(RespawnTime)
			Debounce = false
		end
	end
end)

Is anyone else having this problem?

You aren’t using a while true loop, you’re using RunService.Heartbeat which will have the code run every frame. Exhausted allowed execution time means you’re trying to run code too fast. For every frame, you’re waiting for 10 seconds and checking if a debounce is false. You’re doing this 60 times a second or higher depending on your monitor’s refresh rate and if you’re using an fps unlocker. A while true loop should work fine.

2 Likes

I don’t see a while true do in there?

If you were to use while true do, always write while task.wait() do so you never get timed out.

My bad, this was a different way that I attempted to do.

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local Ball = ReplicatedStorage:WaitForChild("Objects"):WaitForChild("Ball")
local Parent = script.Parent
local RespawnTime = Parent:WaitForChild("RespawnTime").Value

--//Controls
local Debounce = false

--//Loops
while true do
	if not Debounce then
		Debounce = true
		
		local BallClone = Ball:Clone() 
		BallClone.CFrame = Parent.CFrame
		BallClone.Parent = workspace

		local V = BallClone:WaitForChild("LinearVelocity")
		
		task.wait(1)
		V.Enabled = true

		local X = math.random(-1, 1) * 15

		if X == 0 then
			V.VectorVelocity = Vector3.new(X, 0, math.random(-1, 1) * 15)
		else
			V.VectorVelocity = Vector3.new(X, 0, 0)
		end
		
		task.wait(RespawnTime)
		Debounce = false
	end
end

Like what @7z99 said, you’re using a Heartbeat loop and not a while true do loop. These are very different in how they function. No matter how much task.wait()s you add in your Heartbeat loop, the code will still run 60 (or higher) times a second.

From what I understand, you are making a while true do loop every single frame (60 times in a second). Which is actually worse than your last one.

Oh wait, you changed your code. Well, I recommend adding a wait outside the if not debounce statement.

Oh, there’s no need for an if-statement then because you’re going to be yielding the thread for RespawnTime + 1 seconds, it cannot proceed to the next iteration without waiting for the task.waits to be over. This should work:

while true do
	local BallClone = Ball:Clone() 
	BallClone.CFrame = Parent.CFrame
	BallClone.Parent = workspace

	local V = BallClone:WaitForChild("LinearVelocity")
		
	task.wait(1)
	V.Enabled = true

	local X = math.random(-1, 1) * 15

	if X == 0 then
		V.VectorVelocity = Vector3.new(X, 0, math.random(-1, 1) * 15)
	else
		V.VectorVelocity = Vector3.new(X, 0, 0)
	end
	
	task.wait(RespawnTime)
end
1 Like

For some reason, on line 21, the script still seems to error and tell me that the script timed out.

  20:23:14.240  Script timeout: exhausted allowed execution time  -  Server - Spawner:21
  20:23:14.240  Stack Begin  -  Studio
  20:23:14.240  Script 'Workspace.BallSpawner.Spawner', Line 21  -  Studio - Spawner:21
  20:23:14.241  Stack End  -  Studio

Here’s what I put

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local Ball = ReplicatedStorage:WaitForChild("Objects"):WaitForChild("Ball")
local Parent = script.Parent
local RespawnTime = Parent:WaitForChild("RespawnTime").Value

local RunService = game:GetService("RunService")

--//Controls
local Debounce = false

--//Loops

while true do
	local BallClone = Ball:Clone() 
	BallClone.CFrame = Parent.CFrame
	BallClone.Parent = workspace

	local V = BallClone:WaitForChild("LinearVelocity") -- Line 21

	task.wait(1)
	V.Enabled = true

	local X = math.random(-1, 1) * 15

	if X == 0 then
		V.VectorVelocity = Vector3.new(X, 0, math.random(-1, 1) * 15)
	else
		V.VectorVelocity = Vector3.new(X, 0, 0)
	end

	task.wait(RespawnTime)
end

Are you running this in every single ball you’re cloning? I don’t see any reason why this so expensive it exhausts any limits.

Yes, Im making it so that this script that runs in a spawning part spawns every ball from their.

Okay that would do it, you’re creating a ball at the very beginning of the thread which spawns another ball which at the beginning spawns another and so on. You should try referencing externally from server script service.

I tried putting it in ServerScriptService, and the script is still timing out

 20:42:11.145  Script timeout: exhausted allowed execution time  -  Server - BallSpawner:26
  20:42:11.145  Stack Begin  -  Studio
  20:42:11.145  Script 'ServerScriptService.BallSpawner', Line 26  -  Studio - BallSpawner:26
  20:42:11.145  Stack End  -  Studio
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local Ball = ReplicatedStorage:WaitForChild("Objects"):WaitForChild("Ball")
local Parent = game.Workspace:FindFirstChild("BallSpawner")
local RespawnTime = ReplicatedStorage:WaitForChild("Values").RespawnTime.Value

local RunService = game:GetService("RunService")

--//Controls
local Debounce = false

--//Loops

while true do
	if not Debounce then
		Debounce = true
		
		local BallClone = Ball:Clone() 
		
		BallClone.CFrame = Parent.CFrame
		
		BallClone.Parent = workspace

		local V = BallClone:WaitForChild("LinearVelocity")
		
		V.Enabled = true
		
		local X = math.random(-1, 1) * 15
		
		if X == 0 then
			V.VectorVelocity = Vector3.new(X, 0, math.random(-1, 1) * 15)
		else
			V.VectorVelocity = Vector3.new(X, 0, 0)
		end
		task.wait(RespawnTime)
	end
end

That’s very weird, doesn’t make sense to me that it’s exhausting any execution time. What is RespawnTime equal to? Also, you removed the script inside of the ball right? This is the full script, right? If not, could you post it?

This is the Full script, I switched where the script was, Respawn Time is equal to 5 seconds.

OH I think it’s because you aren’t ever setting debounce to false which means it wouldn’t yield after the first iteration. Try removing the if statement again.

Whats that

Also, its still timing out even though I removed the if statement

  21:05:53.422  Script timeout: exhausted allowed execution time  -  Server - BallSpawner:21
  21:05:53.422  Stack Begin  -  Studio
  21:05:53.422  Script 'ServerScriptService.BallSpawner', Line 21  -  Studio - BallSpawner:21
  21:05:53.422  Stack End  -  Studio
--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local Ball = ReplicatedStorage:WaitForChild("Objects"):WaitForChild("Ball")
local Parent = game.Workspace:FindFirstChild("BallSpawner")
local RespawnTime = ReplicatedStorage:WaitForChild("Values").RespawnTime.Value

local RunService = game:GetService("RunService")

--//Controls
local Debounce = false

--//Loops

while true do
	local BallClone = Ball:Clone() 
	BallClone.CFrame = Parent.CFrame
	BallClone.Parent = workspace

	local V = BallClone:WaitForChild("LinearVelocity")

	task.wait(1)
	V.Enabled = true

	local X = math.random(-1, 1) * 15

	if X == 0 then
		V.VectorVelocity = Vector3.new(X, 0, math.random(-1, 1) * 15)
	else
		V.VectorVelocity = Vector3.new(X, 0, 0)
	end

	task.wait(RespawnTime)
end
1 Like

Basically it just refers to the time where code is run in a loop:

for i = 1,10 do
    print('This loop is on the',i,'iteration')
end

Okay, that is so weird. You’re committing changes right? Are you able to provide a repro file? I’ve tried reproducing the issue but it doesn’t exhaust execution time, it spawns a part every ~6 seconds with the code you gave. I might be missing something though.

Here’s what I have:
Baseplate.rbxl (39.4 KB)

nan

wont leave this up for long but here

Also, Im the only person making the game, so team create was never enabled

1 Like

Think I found the issue. It was actually because of the script inside of the ball, there’s a cash script and you have a wait only if the condition is true. I added a few comments:

local Ball = script.Parent.Parent.Parent.Parent

while true do
	if Ball.Size == 1 then
		-- if the task.wait(1) was here, it would only yield if the ball's size was 1, it can never be 1 because ball.Size is a Vector3, Vector3's can never be equal to a number, they can only be equal to another Vector3.
		task.wait(1)
		
		print("yes")
	end
	-- so instead, you should use a task.wait(1) here so it yields regardless of the condition's truthiness
	task.wait(1)
end

So it was this that was causing the error, not the script in server script service. I assume the script that was cloning it was emitting the error because there’s some detection to determine what caused the script to be placed in the workspace.

And the location of the script of course,
image

1 Like

It worked, thanks!

I spent approximately 4 hours finding out how to fix this O_O

1 Like