I am making a zombie game and when the generator dies I want to make it so that the spawn script restart. Its just 1 big spawn script
These methods are nearly one of the same, but you can wrap the entire script into a bindableevent and run the code all over again.
And similarly, easier, and more performanently you can just put your script into a module function and call it as many times as you want and whenever.
edit: op asked for an example.
--a new module script local module = {} local function CodeIwannaRunALOT(self, argument) -- we're gonna use : syntax for this (self) print("hi") end module.CodeIwannaRunALOT = CodeIwannaRunALOT; return module
--some script local mod = require(replicatedStorage.ModuleScript) --run initally. mod:CodeIwannaRunALOT("foo") --once we need to run it again. event.Event:Connect(function() mod:CodeIwannaRunALOT("foo") end)
There, it's the easiest way to do it is by module and modules are good to use anyway.
The answer is, no. There is no function that can restart a script. But! There are certain ways you may restart your scripts with.
1) Disable and enabling the script again.
Disabling a script an re-enabling it is a very common way! Each script in Roblox has a property called "Disabled". Whenever the value is true the script will stop running. This is a very useful way and I can tell you I used it multiplie times. I will give you an example!
function onTouch(hit) if hit.Parent:FindFirstChild("Humanoid") then game.ServerScriptService.Rain.Disabled = true wait(3) game.ServerScriptService.Rain.Disabled = false end end script.Parent.Touched:Connect(onTouch)
This script disables a script that contains a rain that gets stronger and stronger every 3 secounds. Whenever you touch the brick, it will instantly disable the script for 3 secounds, and then enable it. Which will mean the rain will stop and suddenly start again from the less stronger wind.
2) Cloning/Disabling old script (NOT 100% SURE ITS WORKING).
I apologize but when typing this answer I do not have a lot of time to answer before I need to go. So I would not be able to give you an example. Basically. When you clone a script it clones the source of it as well (NOT SURE). By that, you can disable the old script, and make the new one enabled, this will restart the script from the top to the bottom.
I hope this was helpful. XDvvvDX!