I’ve been following several tutorials to create a round-based game which includes roblox’s tutorial on the wiki. They all have a single script running in a loop to manage rounds. If some error were to happen in this loop, the entire script would come to halt. Then the server would completely become useless.
I will try design my code to prevent errors, but let’s say an error does happen when doing functions with players, like changing their team. I could use a pcall or xpcall in areas of code that could have errors, but its probably impossible to squash every bug.
If there is an error in the round manager script causing it to stop I don’t want players to wait around wondering why nothing is happening.
How should I go about handling an error in the round manager script? I was thinking I could put all the entire code in a pcall, and if something goes wrong I could let all players know and kick them all.
There is a service called ScriptContext which tells you if a script has errored, and gives you the error message, stack trace, and the script that errored.
You could try and do something like this with it:
local ScriptContext = game:GetService("ScriptContext")
local ScriptToTrack = script.Parent:WaitForChild("RoundManager") --whatever it's called
ScriptContext.Error:Connect(function(ErrorMessage, Trace, Script)
if Script == ScriptToTrack then
warn(string.format("Error: %s Trace: %s", ErrorMessage, Trace))
--fire a remoteevent or kick everyone
end
end)
What @zQ86 said, as well as what you yourself suggested are both good ways to approach this solution. Realistically however, if you are making a script that you know has a high likelihood of erroring, it’d probably be best to take a step back and reevaluate how you are scripting your game.
In my own personal opinion, I would use pcalls for the simple reason that, in zQ86’s solution, any error, not necessarily a game breaking one, would cause the entire game to shut down. Alternatively, you could use ScriptContext service in order to create a debugging script, one which is activated to disable and re-enabled your round manager script.
Thanks very much for this information. I had no idea you could track and trap a script for errors. I have been bloating my code using individual pcalls to handle error trapping.
local success, errorMessage = pcall(function()
return true
end);
if not success then
return false
end