Error with IsA()

What do I want to achieve?
I’m trying to make a car spawning system for my (experience)

What’s the problem?
When I play test pretty much everything works fine, but pretty much randomly I get an error in the console saying Error indexing nil with IsA() and no new cars can spawn from that player neither can they be deleted.

Here’s the if statement I get the error on:

if player.Character.Humanoid.SeatPart ~= nil and player.Character.Humanoid.SeatPart:IsA("VehicleSeat") or player.Character.Humanoid.SeatPart:IsA("Seat") then
player.Character.Humanoid.Sit = false

Keep in mind, this script worked fine before I added the or part.
This is also inside of a TextButton in StarterGui

1 Like
if player.Character.Humanoid.SeatPart ~= nil and player.Character.Humanoid.SeatPart:IsA("VehicleSeat") then

player.Character.Humanoid.Sit = false

elseif player.Character.Humanoid.SeatPart:IsA("Seat") then

end

I think this might fix it.

1 Like

OHH that makes tons of sense that should work correctly.

1 Like

you can do what @Tekakato2 said or do this

--// Instead of this
if player.Character.Humanoid.SeatPart ~= nil and player.Character.Humanoid.SeatPart:IsA("VehicleSeat") or player.Character.Humanoid.SeatPart:IsA("Seat") then

--// Do this
if player.Character.Humanoid.SeatPart ~= nil and player.Character.Humanoid.SeatPart:IsA("VehicleSeat") or player.Character.Humanoid.SeatPart ~= nil and player.Character.Humanoid.SeatPart:IsA("Seat") then

It’s one big line of code which I personally don’t prefer, but should work aswell.