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

How to continue a loop after someone gets off a seat?

I'm trying to find out how to continue a loop after the player gets off his seat.

I tried writing a loop that sends someone to a detention room, but when I tested it out, I got sat into a chair near by the teleportation location and the loop ended. I want to instead know how to script something that can make sure the loop will continue after the person was seated.

1 answer

✓ Accepted answer

Put this script in the seat:

script.Parent.Changed:Connect(function() -- Connects a function when something changed in the seat
	if script.Parent.Occupant == nil then -- Checks if Occupant is true or false
		repeat -- Repeats a function
		wait() -- Returns a wait function
		until script.Parent.Occupant ~= nil -- Repeats function till somebody is sitting on the seat
	end    
	
        coroutine.resume(coroutine.create(function()
	    while true do -- Creates a loop
		wait(0.01) -- Waits for 0.01 second
		if script.Parent.Occupant ~= nil then -- Checks if the seat is occupied
			local P = game.Players:WaitForChild(script.Parent.Occupant.Parent.Name) or game.Players:GetPlayerFromCharacter(script.Parent.Occupant.Parent) -- Gets the player
			local PT = P.Character.Torso -- Gets the player's torso
			
			if PT then -- If there is a torso then
			    PT.CFrame = workspace.DetentionRoomFloorNameHere.CFrame + Vector3.new(0,3,0) -- Change DetentionRoomFloorNameHere with the detention room's floor name it basically teleports the player to above the detention room floor by 3 spaces
			else -- Else if there is no torso
				-- Nothing happens
			end
		end
	end
end))
end)

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