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.
How Do I Make Scripts Start And Stop A Certain Times?
1 answer
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!
2 comments
I dont understand tho how i would make it run when so and so happens and we its finished happening to then make it stop?
Then don’t wrap the Script in a Loop, that’s if you want it to just run once, or write ‘break’ at the bottom
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
Do you have an attempt to show us? Scripting Helpers is not a request site so if you are asking for a script, please stop.
What complete moron voted against the close vote. Ridiculous.
Incapaz must be mad no one agreed with his other close request.
Can Anyone Help