Need help with moving platform script

Hello! Could someone please debug this script?
What I am trying to achieve: A platform that moves for a certain while when you sit on it.
Info:
-Anchored = true
-CanCollide = true
-The script is a “Script”
-The parent is a “Seat”

(Comment if you need anything to debug it!)

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function() --the function for sitting
	
while true do --loop

local part = script.Parent
		part.Position = part.Position + Vector3.new(0.1, 0, 0) -- part movement
		wait(0.01) -- delay
wait(20) -- time until it stops
		break --stops the loop

	end	
end)

Thanks! 
-Cooper

U can try using the Enum idle, or u can make a animation play when u sit on it

It is not good to put a while loop inside a GetPropertyChangeSignal because once it is changed, it will run forever. So I think you should do a loop like this:

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function() 
    while script.Parent.Occupant ~= nil do
       -- move the platform
    end
end)
1 Like

Where do I add this exactly? Please make it more clear. Like this?

   while script.Parent.Occupant ~= nil then
    	
    while true do --loop

    local part = script.Parent
    		part.Position = part.Position + Vector3.new(0.1, 0, 0) -- part movement
    		wait(0.01) -- delay
    wait(20) -- time until it stops
    		break --stops the loop

    	end	
    end)

Add what in what? It is similar to yours.

Like this?

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function() 
while script.Parent.Occupant ~= nil then


    local part = script.Parent
    		part.Position = part.Position + Vector3.new(0.1, 0, 0) -- part movement
    		wait(0.01) -- delay
    wait(20) -- time until it stops
    		break --stops the loop

    	end	
    end)

Yea like that.
(30 charssssss)

I will test. Thanks! I will let you know if it works!

It worked! Had to change the “then” for a “do” and it worked!

Script:

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function() 
while script.Parent.Occupant ~= nil do


    local part = script.Parent
    		part.Position = part.Position + Vector3.new(0.1, 0, 0)
	    		wait(0.01)
end	
    end)