Prevent it from breaking the script if the player character dies in the middle of the code while it's running

So basically, there’s lines of code that involve a player character and the thing is, is that when it run, sometime the player dies in the middle of the code while it’s running, this will break the script and it will print out error messages. So I don’t exactly know what to do, to prevent this from breaking the script and printing out errors, I thought of putting a if statement to check if the character isn’t nil but I can’t do that every time there’s a line of code that involve a player character.

Right okay, can you post the code?

1 Like

For example

character.HumanoidRootPart.Position = 100,100,100

lets say the character dies when it get to that line, the script would break because the character and the humanoidrootpart are nil .

It’s fine to do a check every time you need to access a property of the Character model or Humanoid. Just do something like:

local char = player.Character
if char then
    local humanoid = char:FindFirstChild("Humanoid")
    if humanoid then
        --Your code here

If you have multiple places where this is necessary in your code you might consider transplanting the check into a ModuleScript:

local player = game.Players.LocalPlayer

local CharacterFuncs = {}

function CharacterFuncs.HasCharacterAndHumanoid()
    if player.Character then
        if player.Character:FindFirstChild("Humanoid") then
            return true
    return false

return CharacterFuncs

Then the first bit of code I wrote might look something like this:

local CharFuncs = require(player.PlayerScripts.CharacterFuncs)

if CharFuncs.HasCharacterAndHumanoid() then
    --Code here is safe to assume the local player has a Character and that Character has a Humanoid

1 Like

I mean, I can go with it but is there a more efficent way or not, thanks.

Really depends on the context behind how your code is implemented and what you’re trying to accomplish. Could we get an idea of what you’re trying to do?

1 Like
   local function Fling(player,hit,Tsunami)
    	local Animation = game.ReplicatedStorage.Animations.Fling
    	local Fling = hit.Parent.Humanoid:loadAnimation(Animation)
	local chrPos = hit.Parent.PrimaryPart.Position 
	local tPos = Tsunami.Position 
	local newCF =,tPos) 
	local BodyVelocity ="BodyVelocity")
	BodyVelocity.MaxForce =,math.huge,math.huge)
	BodyVelocity.Velocity = hit.Parent.HumanoidRootPart.CFrame.lookVector * -40
	BodyVelocity.Parent = hit.Parent.HumanoidRootPart
	hit.Parent.Humanoid.AutoRotate = false
	hit.Parent.Humanoid.PlatformStand = true
	game.Players:FindFirstChild(hit.Parent.Name).Stun.Value = true
	game.Players:FindFirstChild(hit.Parent.Name).Stun.Value = false
	hit.Parent.Humanoid.AutoRotate = true
	hit.Parent.Humanoid.PlatformStand = false

basically here’s a example, I’m trying to make a fling/stun thing and it get the hit.Parent character a lot, and sometime that character dies in the middle of running the script and it would break the script.

The reason you can sometimes get the error is because of the wait() statement in that function. You can either perform a check both at the beginning of the function and immediately after the wait() call, OR (and I’d reccommend this way for you context) you can wrap the body of the function in a pcall(). pcall(), if you’re unfamiliar is short for “protected call” and is Lua’s way of catching exceptions. It might look something like this for you:

local function Fling(player,hit,Tsunami)
    local success, msg = pcall(function()
        local Animation = game.ReplicatedStorage.Animations.Fling
        --all the code inbetween the line above and the line below
    --If you need, you can then check whether the code ran without errors here:
    if success then
        --The code ran without errors
        --There was an error, 'msg' will be a string of what the error message would have been
        --Likely, for your case, you won't need to do anything and can leave this section blank