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
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.
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)