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

Why isn't my seat function returning "Seat Occupied" when it is sit on?

Asked by 6 years ago

When I get on the seat, it prints Humanoid, then when I jump off of it, it prints nil then Seat Occupied. How do I get it to print "Seat Occupied" when I sit on it?

    seat.Changed:connect(function()
        print(seat.Occupant)
        if seat.Occupant == Humanoid then
            print("Seat Occupied")
        end
    end)
0
What is Humanoid defined as? User#19524 175 — 6y
0
When I sit in the seat, the value of Occupant is "Humanoid" CoosBuckett 10 — 6y

1 answer

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

Hi Coos,

It doesn't print "Seat Occupied" because the Occupant is an object property. So, if Humanoid is a variable that is set to the string "Humanoid" then, it won't print because the Occupant is actually an object. If you however check seat.Occupant.Name == "Humanoid" then it would work, or seat.Occupant:IsA("Humanoid") then it may work. Also, I'm not sure if you defined what Humanoid is above in the script. Hopefully you also defined seat.

So, it would look something like this:

local seat = script.Parent;

seat.Changed:Connect(function()
    print(seat.Occupant)
    if seat.Occupant:IsA("Humanoid") then
        print("Seat Occupied")
    end
end)

Hope i helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

0
Using IsA to check if an object is of that ClassName isn’t a good idea. IsA returns not only that class, but also classes that inherit from it. If a class that inherits from Humanoid is sitting on the seat, it will also run code. User#19524 175 — 6y
0
First off, IsA returns a boolean value. It checks if the object is of the class specified in the parameter or if it inherits from that class, but seeing as no class inherits Humanoid, it's perfectly safe to use IsA since it would only return true if it's a Humanoid. KingLoneCat 2642 — 6y
Ad

Answer this question