Chair won't kill player (humanoid parent is nil?)

The error I have:
15:48:41.918 - Workspace.KINGCHAIR.Seat.Script:1: attempt to index nil with 'Parent' 15:48:41.919 - Stack Begin 15:48:41.919 - Script 'Workspace.KINGCHAIR.Seat.Script', Line 1

The code I used:

local char = script.Parent.Occupant.Parent
repeat wait() until char

local plr = game.Players:GetPlayerFromCharacter(char)

if plr.TeamColor ~= BrickColor.new("Deep orange") then
	
	char.Character.Humanoid.Health = 0
end

My goal is to create a chair when someone that is not in the team of King or has the team color of deep orange, they die unless they are in the team of King or do have the team color of deep orange.

Does anyone know how to fix this scripting problem? Thank you for reading.

That’s because you referred to a Parent who didn’t exist here:

local char = script.Parent.Occupant.Parent
1 Like

local plr = game.Players:GetPlayerFromCharacter(script.Parent.Occupant.Parent) Maybe try that instead of how you defined character.

Edit: Eh, I realized that isn’t the best either, but it’s worth a try.

This is due to the first like in your code— you wrote “Occupant.Parent,” but the thing is that the chair doesn’t have an occupant until someone sits in it. Therefore, doing Occupant.Parent causes an error. Change it to simply say “occupant,” and then after the repeat until loop, you can then grab the occupant parent

1 Like

I recommend chaning your code to something like this:

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
   local plr = game.Players:GetPlayerFromCharacter(script.Parent.Occupant.Parent)

   if plr and plr.TeamColor ~= BrickColor.new("Deep orange") then
	   plr.Character.Humanoid.Health = 0
    end
end)

This will fire when someone sits.

3 Likes