Trying to sit in seat by removing NPC

I have a vehicle system that let’s you steal cars. And I can’t steal the vehicle, or for that matter any seat with an NPC in it. I can access a seat that has no player in it, but if I try to access the seat with an NPC in it, my character gets stuck and freezes up, can’t move.

Error:
Attempt to index nil with AncestryChanged

I’ve tried using .Changed and so many other methods of troubleshooting this. No luck, character just gets stuck. I’m at a loss and frusturated.

Code:

function DeleteScript()
	ContextActionService:UnbindAction("ExitVehicle")
	ContextActionService:UnbindAction("SpaceLock")
	script:Destroy()
end

Seat.AncestryChanged:Connect(function()
	if Seat.Parent == nil then
		DeleteScript()
	end
end)

Humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function()
	if Humanoid.SeatPart ~= Seat then
		DeleteScript()
	end
end)

Your error means that the code tries to get the AncestryChanged property of a nil variable.

Based on the code you provided, that probably means Seat is nil:

Seat.AncestryChanged

With the part of the code where Seat is defined I might be able to be of more help.


If it’s only for seats that are already occupied, you could try removing the NPC in the seat by doing something like:

-- Check if there is a humanoid in the seat and it's not the player's humanoid
if seat.Occupant and seat.Occupant ~= myHumanoid then
	-- Remove the other humanoid from the seat
	seat:Sit(nil)
	-- After removing the other humanoid, have the player sit
	seat:Sit(myHumanoid)
end

With the part of the code where Seat is defined I might be able to be of more help.

Sure thing,

local Seat = Humanoid.SeatPart

1 Like

This means that the code is assuming that the Humanoid is sitting in a seat, but they actually aren’t sitting (or at least they don’t have a Seat that they’re sitting in).

Are you doing some logic (or responding to an event) that allows the code to assume the humanoid is sitting?

Key Points:

  1. Indexing nil with AncestryChanged: This error happens if the Seat variable is not properly assigned, or the Seat is nil at the moment the event is being connected. Double-check how you’re defining or referring to the Seat.
  2. Player Stuck with NPC: You need to handle the scenario where the NPC is seated before the player, and the player attempts to take the same seat.

Ensure Seat is Valid

Before connecting the AncestryChanged event, ensure that the Seat variable is valid and correctly assigned.

-- Ensure Seat is properly assigned
local Seat = script.Parent:FindFirstChild("Seat") -- Or however you're defining the seat
if not Seat then
    warn("Seat not found!")
    return -- Safeguard against nil
end

Check for Existing NPC and Remove

local function DeleteScript()
    ContextActionService:UnbindAction("ExitVehicle")
    ContextActionService:UnbindAction("SpaceLock")
    script:Destroy()
end

-- Function to handle seat occupation
local function HandleSeatOccupation()
    local occupant = Seat.Occupant -- Check if something is occupying the seat (NPC or other)
    
    if occupant and occupant.Parent:FindFirstChild("Humanoid") then
        -- Check if occupant is an NPC
        if occupant.Parent ~= game.Players.LocalPlayer.Character then
            -- Eject the NPC or remove them from the seat
            occupant.Parent.Humanoid:Sit(false)
        end
    end
end

-- Monitor when the seat is changed or an NPC leaves
Seat.AncestryChanged:Connect(function()
    if Seat.Parent == nil then
        DeleteScript()
    end
end)

-- Monitor changes in SeatPart to ensure proper behavior
Humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function()
    if Humanoid.SeatPart ~= Seat then
        DeleteScript()
    end
end)

-- Call this function when the player tries to sit in the seat
HandleSeatOccupation()