For instance i have a game where you have to defend yourself from enemies in my case zombies and once you lose all your health you become knocked and its starts playing a crawling animation which i am also unsure how to do but then after a team comes to heal you its stop the animation and start standing again and running like normal.
If you want the Script to repeat forever, then use while
or repeat until
, the first method can run however many times you like, can also have a circumstance required for the loop to run, repeat
can also do this, yet both will immediately break whenever the given circumstance is no longer true. This would look like so
while true wait(BeforeNextRun) do --// it’s crucial you include a wait() as the loop can run too fast and cause the Studio to break local Part = Instance.new(“Part”); Part.Parent = workspace if (part ~= nil) then local CurrentCFrame = Part.CFrame Part.CFrame = Part.CFrame * Part.CFrame() + Vector3.new(CurrentCFrame.p) end end
This will run forever, every time[BeforeNextRun]. If it’s a conditional statement, it would look like this
local Boolean = true spawn(function() while Boolean wait(BeforeNextRun) do local Part = Instance.new(“Part”); Part.Parent = workspace if (part ~= nil) then local CurrentCFrame = Part.CFrame Part.CFrame = Part.CFrame * Part.CFrame() + Vector3.new(CurrentCFrame.p) end end end) --// This will run until ‘Boolean’ is no longer true, you’ll also notice ‘spawn(function() end)’ that separates the loop and the rest of the Script, allowing the loop to run whilst not interrupting anything wait(SameTimeAsBeforeNextRun * 3) --// After three loops then... Boolean = false --// Notcie the loop stopped
You can also use break
, this works for both methods. It can be called wherever inside the loop, yet be careful as break
immediately breaks whichever loop it is called in. Sometimes if you have more than one and you call this method, whichever loop it was inside it will break the corresponding one, it will not break the first one.
Then you have repeat
, it would look like this
local Boolean = true spawn(function() repeat wait(BeforeNextRun) local Part = Instance.new(“Part”); Part.Parent = workspace if (part ~= nil) then local CurrentCFrame = Part.CFrame Part.CFrame = Part.CFrame * Part.CFrame() + Vector3.new(CurrentCFrame.p) end until not Boolean --// Get’s opposite of ‘Boolean’, that would be false end) wait(SameTimeBeforeNextRun * 3) Boolean = false
Unlike while
, return
always requires a conditional statement. Yet you can use return
as a Yield, this would usually be seen at the top of a Script yet can be applied anywhere, this method is used to yield the Script until curtain requirements are met, would look like so
repeat wait() until --// circumstance
Hope this helps, spent a while putting this together, if so, don’t forget to Upvote and Accept this Answer! Good luck!
Closed as Not Constructive by TheeDeathCaster
This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.
Why was this question closed?