How to reset server script?

My game has a main server script that runs the game timer, intermission, teleport to play area, winners, etc, but sometimes it breaks for a variety of reasons, and it would be much easier to reset the whole script in game than to fix each and every bug so that they don’t happen again. Is there any way to reset/restart a server script, (preferably) from another script? My plan would be for another script to check the first one, and if it is not moving on to the next part of the game when it should, it will reset it.

The only think I can think of is to teleport all players to a different server, but it seems very inefficient and I would rather restart the script.

1 Like

You could probably just clone the script from serverstorage to start it, then destroy and reclone it to restart it. But you would possibly have to modify the script so it doesn’t break, however, if you provide the part of the script that’s breaking, I might be able to give you a better solution.

2 Likes

Oh ok, I will try that.

I was about to make a post about some of the problems I was having, but I really dont have enough info on them, and they are rare enough that it isnt a huge problem, but it is just a thousand little problems that I can never replicate again. Like if a player happens to leave while the script is trying to teleport them, there is an infinite wait on waitForChild. Or for some reason it might think a player is still playing when they are not, or some other random thing. I think it is easier to just restart it with a monitor script if something goes wrong, or to give myself some sort of in game command to restart it.

1 Like

It would probably be better practice if you fix the consistent errors, either on your own or by posting them here, or wrapping them in a pcall to silence the errors, and you can have custom error handling.

You could also just make the entire code run in a function and call the entire function in a pcall, and if there’s an error it prints the error then restarts the function.

1 Like

I know it would be better, but it is stuff like I have no idea how it happened, it will most likely never happen again, and it is impossible to recreate. I have rewritten my main script from scratch 4 times to get rid of all the bugs I can.

The function idea is really smart, I might do that, thank you.
I have heard of some auto indent feature in studio, do you know how to use that, because it would really help when I have to add an indent to my whole code with the function.

if you select all the code with ctrl + a, and just press tab once it should indent it

1 Like

Do I need to stop the function somehow before I call it again, because it has a forever loop in it?

Also ty for the indent thing

Rather than reloading the entire script use pcall to catch any errors and retry

function YourScript()
local S , F = pcall(function()
– Code to run
end)
if S then
– No issues
end
if F then
– Got an error, restart YourScript()
end
end

Thats what I am working on right now, based on what QuantixDev said. I need some sort of thing though, like the server needs to return a variable every so many seconds, and if it doesn’t then the function is restarted. Because a lot of it isn’t outright errors, but just some small thing, with like bad timing for a player to leave or whatever, but it holds up the script, without any true errors. I am going to work on it tomorrow and will see if it works.

Pcall is also a bad idea. It should really only be used for errors beyond your control.

1 Like

What should I do then? Will it harm my script in any way to have a pcall?

I’m not sure on the performance impact, but it’s a bad practice. It’s hugely frowned upon to say “my code has bugs, let’s hide them all”. It can cause more problems down the road, especially if you have a different bug pop up in the same code. Do what you will, your game won’t die because of this. I do wish people wouldn’t recommend it though, the real life workforce doesn’t like seeing this kind of workaround. Relevant XKCD

3 Likes

you mean like this?

function YourScript(code, ...)
  local success, error = pcall(code, ...) -- running
  return success --[[ and print("yay") ]] or YourScript(code, ...) -- loop???
end

I wouldnt go about it like that, usually all my pcalls run within the script rather than as a function. Its just as a temporary thing until the bugs are fixee that i would suggest using it on functions

1 Like

I could take out the pcall and just do some sort of “check up ping” (really not a good way of describing it, sorry) where my function has to return a variable every x seconds (maybe increasing numbers or something, if it doesn’t after like 10 seconds, the function is restarted. I will probably try to do that anyway, but should I take out the pcall then?

No, I did it outside the function