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:
1 | Model |
2 | Seat |
3 | Script |
4 | Parts |
5 | Part |
6 | Part |
7 | ... |
Script:
1 | script.Parent:GetPropertyChangedSignal( "Occupant" ):Connect( function () |
2 | if script.Parent.Occupant ~ = nil then |
3 | print ( "sit" ) |
4 | return |
5 | elseif script.Parent.Occupant ~ = not nil then |
6 | wait( 5 ) |
7 | script.Parent.Parent:remove() |
8 | end |
9 | 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:
1 | script.Parent:GetPropertyChangedSignal( "Occupant" ):Connect( function () |
2 | if script.Parent.Occupant ~ = nil then |
3 | print ( "sit" ) |
4 | return |
5 | elseif script.Parent.Occupant ~ = not nil then |
6 | script.Parent.Parent:Destroy() |
7 | end |
8 | end ) |
Or if you want to use a repeat loop (which isn't essential in this case) you can do it like so:
1 | script.Parent:GetPropertyChangedSignal( "Occupant" ):Connect( function () -- checks whether the 'Occupant' property has changed. |
2 | if script.Parent.Occupant ~ = nil then -- if there is someone sitting |
3 | print ( "Someone sat down in the seat" ) |
4 | repeat wait() -- this will repeat and it'll wait approximately 0.03 to 0.007 seconds |
5 | until script.Parent.Occupant = = nil -- until there is no one sitting in the seat |
6 | script.Parent.Parent:Destroy() -- destroys the model |
7 | end |
8 | end ) |
Instead of using :remove, :Destroy() is better since remove is outdated/deprecated, meaning it might not be supported in the future!