Kick off of seat script not working

For some reason my script that kicks people off the seat is not working the script is in the seat.
code:

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
	local player = game.Players:FindFirstChild(script.Parent.Occupant.Parent.Name)
	print(player.Name)
		local human = script.Parent.Occupant
		script.Parent.SeatWeld:Destroy()
		human.Sit = false
end)

attempt to index nil with parent

I believe you have to make a check to see if there’s a value in Occupant, since even if it changes to nil, it will still activate the event. So you’d have to do

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
	if script.Parent.Occupant then
		local player = game.Players:FindFirstChild(script.Parent.Occupant.Parent.Name)
		print(player.Name)
		local human = script.Parent.Occupant
		script.Parent.SeatWeld:Destroy()
		human.Sit = false
	end
end)

This should hopefulyl work as if there’s nothing in Occupant, nothing will get ran

2 Likes

Still does not work also the players name is not getting printed even if they are actually sitting in the chair. I don’t think the function is even getting ran.

Maybe it’s

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local hum = seat.Occupant
	if hum then
		local char = hum.Parent
		local player = game.Players:GetPlayerFromCharacter(char)
		print(player.Name)
		seat.SeatWeld:Destroy()
		hum.Sit = false
	end
end)

Try this out, haven’t tested but hopefully you can

local seat = script.Parent

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if not seat.Occupant then return end
	local character = seat.Occupant.Parent
	
	local humanoid = character:WaitForChild("Humanoid")
	if humanoid then
		wait(0.1)
		humanoid.Sit = false
		humanoid.Jump = true
	end
end)

I fixed it thx. charsdobelookinhot