Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

When player jumps off the seat, model is removed?

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!

1 comment

0

Try removing the wait(5) so it instantly removes it. Also :remove() is deprecated use :Destroy() instead

1 answer

✓ Accepted answer

##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

3 comments

0

wait() typically returns somewhere from 0.03 to 0.7 seconds

0

This works, but you do not need a loop, in this case.

0

Re-edited your script

This discussion is preserved as it was. New answers, comments, and votes are closed.