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

When player jumps off the seat, model is removed?

Asked by 3 years ago
Edited 3 years ago

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!

0
Try removing the wait(5) so it instantly removes it. Also :remove() is deprecated use :Destroy() instead JesseSong 3916 — 3y

1 answer

Log in to vote
2
Answered by 3 years ago
Edited by JesseSong 3 years ago

Problem:

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)

Recommendation(s)

Instead of using :remove, :Destroy() is better since remove is outdated/deprecated, meaning it might not be supported in the future!

Re-edited by JesseSong

0
wait() typically returns somewhere from 0.03 to 0.7 seconds JesseSong 3916 — 3y
0
This works, but you do not need a loop, in this case. JesseSong 3916 — 3y
0
Re-edited your script JesseSong 3916 — 3y
Ad

Answer this question