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.
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
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)