Still have questions? Join our Discord server and get real time help.
Log in to vote
1

# Is there a function to make a script restart?

Asked by 5 months ago

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

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 IcyMizu 124 — 5mo
0
Yh but what is the restart function for the script FluffySheep46209 369 — 5mo

### 2 answers

Log in to vote
1
Answered by
Gojinhan 353
5 months ago
Edited 5 months ago

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.

1
Thx can you go a little more in depth or give a quick example, thx FluffySheep46209 369 — 5mo
1
yeah Gojinhan 353 — 5mo
1
thx FluffySheep46209 369 — 5mo
1
I edited my answer Gojinhan 353 — 5mo
View all comments (3 more)
1
thx FluffySheep46209 369 — 5mo
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 FluffySheep46209 369 — 5mo
0
This code will only perform once try adding a loop. JesseSong 3372 — 5mo
Log in to vote
1
Answered by
XDvvvDX 186
5 months ago
Edited 5 months ago

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!

0
ill try it FluffySheep46209 369 — 5mo