Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Whenever I try to run a while loop , I get an error message saying "Game script timeout?"

Asked by 5 years ago
Edited by User#24403 5 years ago

So basically I'm working on a rocket ship (I'm not the best with lua and have just started to learn it) but I have this script in the block which I want to boost the rocket ship into the sky.

while true do
    game.Workspace.Rocket.Boosters1.Velocity = game.Workspace.Rocket.Boosters1.CFrame.LookVector * 300
end

This script works once, however it does not loop like I want it to do, and before the script is executed the game freezes for a few seconds and tells me "Game script timeout"

I have tested the game through both Run, play, and physically starting up a server.

Really sorry if this question doesn't abide by the rules, however I tried to read through them.

0
add a wait() LoganboyInCO 150 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago

the principle behind a while loop is to repeat the given code until the condition is no longer met. This fails because it repeats the code as fast as possible and lets no other code run, in result, freezing Studio.

In your application, it looks best to do this in RenderStepped, which the code is ran each time a frame is rendered (60 times per second).

game:GetService("RunService"):BindToRenderStep("Rocket", 5, function()
    game.Workspace.Rocket.Boosters1.Velocity=game.Workspace.Rocket.Boosters1.CFrame.lookVector * 300
end)
0
Thank you! Alexanders1123 16 — 5y
Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago

The purpose of a loop is to repeat the Code nested until the provided Condition is no longer met; until a given time runs out; until a value is false.

These loops run extremely fast, sometimes even 0 milliseconds. The Studio cannot handle this. Therefore it’s protocol to timeout a Script that is causing this issue, to prevent any further problems.

The solution for this is to provide a delay in the Code, to let the Script rest, and the quickest time preffered to prevent this issue should be .3 seconds. So add a wait(.3) before the eof.

Log in to vote
0
Answered by 5 years ago
while true do
--Code
wait (Second)
end-

This should work

Answer this question