Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to tell when a player has sat in a seat?

Asked by 3 years ago

Im just starting to mess around with vehicles but I cant seem to understand how to tell when a player is sitting on a seat this is what I tried:

Seat.Occupant.Changed:Connect(function()
    if Seat.Occupant == Instance.new("Humanoid")then
        print("player sat")
    else
        print("player got up")
    end
end)

But I only get the error " attempt to index nil with 'Changed'".

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

The Changed event is located in the seat, not Occupant (which is a property of the object). You could do this:

Seat.Changed:Connect(function()

but this will fire every time anything about the seat changes, so you can only listen for when the "Occupant" property changes with this:

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()

Also, the if statement will always be false because rather than checking if the type of Seat.Occupant is a Humanoid, you are checking if Seat.Occupant is the new Humanoid that you just created. Do this instead:

-- nvm this will give an error oops
if Seat.Occupant:IsA("Humanoid") then
    print("player sat")
else
    print("player got up")
end

edit: I just realized this will give an error when you get up from the seat because Seat.Occupant will be nil, so :IsA() won't exist. You should instead just check if Seat.Occupant is nil or not, but I will still leave this up to show how to check types of Roblox objects

-- do this
if Seat.Occupant then
    print("player sat")
else
    print("player got up")
end
Ad

Answer this question