Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a function to make a script restart?

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

2 comments

0

Do it in a function and then check if the generator idk what u use lets say value changes u fire the respawn script

0

Yh but what is the restart function for the script

2 answers

✓ Accepted answer

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.

7 comments

1

Thx can you go a little more in depth or give a quick example, thx

1

yeah

1

thx

1

I edited my answer

1

thx

0

Would you be able to up -- next to some of the lines to explain what they do bc I'm finding it a bit confuzing

0

This code will only perform once try adding a loop.

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!

1 comment

0

ill try it

This discussion is preserved as it was. New answers, comments, and votes are closed.