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

Running code when a Seat has an Occupant?

Asked by 5 years ago
Edited 5 years ago

I put this script in a Seat. I want to make code run when the Seat has an Occupant(someone is sitting in it) but it doesn't seem to work.

while true do
if script.Parent.Occupant == "Humanoid" then
--Code
end
wait(1)
end
0
You're trying to compare whether an Instance is equal to "Humanoid" Robloxian_Thinking 5 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

Your code will never run as Occupant is the humanoid instance or nil if no humanoid is sitting on the seat. You then compare this object against the string which will never evaluate to true thus the code will never run.

There is however two ways you can run code upon seated. The first like you have done is to check the seat itself. The second is to use the Seated event.

When coding things like this look for the appropriate event and not use loops like this as they are very inefficient. If something looks complicated or verbose then this is an indication that your approach to the problem needs to change.

If you wish to use the seat itself I would use the GetPropertyChangedSignal wich will create an event listener for the given property changed. This is very similar to using Changed.

Example using Changed and GetPropertyChangedSignal

script.Parent.Changed:Connect(function(prop)
    if prop == 'Occupant ' then
        -- run code 
    end
end)

script.Parent:GetPropertyChangedSignal('Occupant'):Connect(function()
    -- code
end)

If you feel it is better to use the humanoid than the seat then you would use the Seated event and check which seat and if they should run any code.

Example using Seated

local seat1 = [path to seat]

humanoid.Seated:Connect(function(isSeated, seat)
    if seat == seat1 and isSeated then -- humanoid sat and is seated on seat1
        -- run code
    end
end)

I hope this helps. Please comment if you ahve any other questions.

0
Your script that uses Changed and GetPropertyChangedSignal worked. Thank you very much. ViktorRamstrom 15 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Avoid while true loops whenever you can!

The best way to do this is by checking when the Occupant property changes, using GetPropertyChangedSignal (to filter out which property we listen to when changed):

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
    local Occupant = script.Parent.Occupant

    if Occupant then --if the Occupant variable isn't "nil", then continue the code
        --Code
    end
end)
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()

end)

Is just a modified version of the Changed event (which fires each time a property in an Instance gets changed):

script.Parent.Changed:Connect(function(PropertyName)
    --PropertyName is the name of the property that changed
    --for example, it can be:
    --Name
    --Parent
    --And everything you see in the Properties tab when you select something in the Explorer

    if PropertyName == "Occupant" then --if the property that changed is the Occupant

    end
end)

Answer this question