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

How do I break this loop when it finds an an empty value?

I am trying to make a part teleport the player to a part (lets call this door) that is not taken, so that if multiple players touch "door" it will line them up instead of teleporting a player to a position that is taken, but when I run this code it does not stop, and instead goes through all of the parts stopping at the last one.

local Door = script.Parent.Parent
Door.TeleIn.Touched:Connect(function(p)
	local plr = game.Workspace:WaitForChild(p.Name)
	wait()
	if closed.Value ~= true then -- if it should teleport the player
		for _,v in pairs (spots:GetChildren()) do -- gets the 'spots' to teleport to
		if v.using.Value ~= true then -- if that spot is empty
			plr.Humanoid.WalkSpeed = 0
			plr.HumanoidRootPart.CFrame = CFrame.new(v.Position) -- teleports player
			v.using.Value = true -- sets that position as taken
			break -- this should stop the loop where it is
		end
	end
	end
end)

1 comment

0

Maybe you're accidentally triggering the touch event several times - try adding some debounce.

1 answer

✓ Accepted answer

Add some debounce:

local touch = false
local Door = script.Parent.Parent
Door.TeleIn.Touched:Connect(function(p)
	if not touch then
		touch = true
		--do your stuff
		wait(1) --waits 1 second before allowing another touch event to do anything
		touch = false
	end
end)

This will still trigger as many times as it did before, but the difference is that debounce only allows stuff you want to happen once.

4 comments

0

well, now it waiting one second before going to the next part, and its still looping through all of them :/

0

It really should not be possible at all for it to do that, make sure you put everything where it should go.

0

@TheGreenSuperman What exactly did you do when you tried my solution?

0

I fixed it with your help and a bit of tinkering, thanks anyways!

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