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)
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.
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.
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
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
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?
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.
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
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.
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.