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