Hi, I would like to make it so that when the player jumps off the seat, the model is removed, but I have difficulties. My script works perfectly, but after I jump off and sit back down, the model is still removed after a while.
The problem is, no matter how much I tried to do it differently, the script stopped working, or nothing changed.
What i tried:
Turn off/on script
Copy, remove and paste back script
Return
My model hierarchy looks like this:
Model Seat Script Parts Part Part ...
Script:
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function() if script.Parent.Occupant ~= nil then print("sit") return elseif script.Parent.Occupant ~= not nil then wait(5) script.Parent.Parent:remove() end end)
Please, help!
The reason it didn't work instantly, was because of the function :Wait
Solution:
All you have to do is remove the wait, and it will work
Fixed Script:
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function() if script.Parent.Occupant ~= nil then print("sit") return elseif script.Parent.Occupant ~= not nil then script.Parent.Parent:Destroy() end end)
Or if you want to use a repeat loop (which isn't essential in this case) you can do it like so:
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function() -- checks whether the 'Occupant' property has changed. if script.Parent.Occupant ~= nil then -- if there is someone sitting print("Someone sat down in the seat") repeat wait() -- this will repeat and it'll wait approximately 0.03 to 0.007 seconds until script.Parent.Occupant == nil -- until there is no one sitting in the seat script.Parent.Parent:Destroy() -- destroys the model end end)
Instead of using :remove, :Destroy() is better since remove is outdated/deprecated, meaning it might not be supported in the future!