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

My while loop is not repeating and there is not any error messages. Can someone help?

The While loop isn't working on line 45 - 54. It doesn't run and there are no error messages. This is the script: (Sorry for the fact that it is not indented properly, i pasted in into here and it did that.)

local CollectionService = game:GetService("CollectionService")
local tag = "Teleporter"

for _, object in pairs(CollectionService:GetTagged(tag)) do
	spawn(function()
		local Players = game:GetService("Players")
		local Workspace = game:GetService("Workspace")
		local ReplicatedStorage = game:GetService("ReplicatedStorage")
		
		local Model = object.Parent
		local Script = script
		
		local OutsideSpawnPoint = Model:WaitForChild("OutsideSpawnPoint")
		local InsideSpawnPoint = Model:WaitForChild("InsideSpawnPoint")
		local Counter = Model:WaitForChild("Counter")
		local Configuration = Model:WaitForChild("Configuration")
		local Moniter = Model:WaitForChild("Moniter")
		
		local GlobalAddPlayer = ReplicatedStorage:WaitForChild("AddPlayer")
		local GlobalRemovePlayer = ReplicatedStorage:WaitForChild("RemovePlayer")
		local GlobalPlayerAdded = ReplicatedStorage:WaitForChild("PlayerAdded")
		local GlobalPlayerRemoved = ReplicatedStorage:WaitForChild("PlayerRemoved")
		
		local mapcollection = {5131207726,5133272479,5135288464}
		local WaitingPlayers = {}
		local CanJoin = true
		local CountingDown = false
		local currentmapid = mapcollection[math.random(1,#mapcollection)]
		Configuration.Mapnumber.Value = currentmapid
		print(currentmapid)
		
		function teleportPlayers()
			local PlaceID = currentmapid
			local TS = game:GetService("TeleportService")
			local Players = game:GetService("Players")
			local code = TS:ReserveServer(PlaceID)
			local players = {}
			for _, v in pairs(WaitingPlayers) do
				table.insert(players, Players:WaitForChild(v))
			end
			TS:TeleportToPrivateServer(PlaceID, code, players)
			WaitingPlayers = {}
		end

if CountingDown == false then
	while false do
		wait(1)
		if CountingDown == false then
			currentmapid = mapcollection[math.random(1,#mapcollection)]
                        print(currentmapid)
			break
		end
	end
end
		function startCountDown()
			if not CountingDown then
				local CanSend = true
				CountingDown = true
				
				for i = 30, 0, -1 do
					Counter:WaitForChild("BillboardGui"):WaitForChild("TextLabel").Text = "  ".. i.. "  "
					if #WaitingPlayers <= 0 then
						CanSend = false
	                           end
						break
					end
					wait(1)
				
				if CanSend then
					CanJoin = false
					wait(1)
					Counter:WaitForChild("BillboardGui"):WaitForChild("TextLabel").Text = "Teleporting..."
					teleportPlayers()
					wait(5)
					currentmapid = mapcollection[math.random(1,#mapcollection)]
					print(currentmapid)
					Counter:WaitForChild("BillboardGui"):WaitForChild("TextLabel").Text = "Waiting For Players..."
					CanJoin = true
				else
					Counter:WaitForChild("BillboardGui"):WaitForChild("TextLabel").Text = "Waiting For Players..."
				end
				CountingDown = false
			end
		end

		function removePlayer(Player)
			for num, item in pairs(WaitingPlayers) do
				if item == Player.Name then
					table.remove(WaitingPlayers, num)
					GlobalPlayerRemoved:FireClient(Player)
					if Workspace:FindFirstChild(Player.Name) then
						if Workspace[Player.Name]:FindFirstChild("Humanoid") and Workspace[Player.Name]:FindFirstChild("HumanoidRootPart") then
							Workspace[Player.Name].HumanoidRootPart.CFrame = OutsideSpawnPoint.CFrame
						end
					end
					break
				end
			end
		end
		
		function addPlayer(Player)
			if CanJoin == true then
				local Passed = false
				if #WaitingPlayers <= 0 then
					Passed = true
				else
					for _, item in pairs(WaitingPlayers) do
						if item == Player.Name then
							Passed = false
							break
						else
							Passed = true
						end
					end
				end
				if Passed then
					table.insert(WaitingPlayers, Player.Name)
					GlobalPlayerAdded:FireClient(Player)
					if Workspace:FindFirstChild(Player.Name) then
						if Workspace[Player.Name]:FindFirstChild("Humanoid") and Workspace[Player.Name]:FindFirstChild("HumanoidRootPart") then
							Workspace[Player.Name].HumanoidRootPart.CFrame = InsideSpawnPoint.CFrame
						end
					end
				end
			end
		end
		
		function findNearByPlayer()
			for _, v in pairs(Workspace:GetChildren()) do
				if v.Parent and v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") then
					if (Counter.Position - v.HumanoidRootPart.Position).magnitude < Configuration:WaitForChild("Distance").Value then
						if Players:FindFirstChild(v.Name) then
							addPlayer(Players:WaitForChild(v.Name))
						end
					end
				end
			end
		end
		
		GlobalAddPlayer.OnServerEvent:Connect(function(Player)
			addPlayer(Player)
		end)
		
		GlobalRemovePlayer.OnServerEvent:Connect(function(Player)
			removePlayer(Player)
		end)
		
		spawn(function()
			while wait() do
				findNearByPlayer()
				if #WaitingPlayers >= 1 then
					startCountDown()
				end
			end
		end)
	end)
end

If you need any other information, please ask!

2 answers

✓ Accepted answer

while false do will never run.

Code in a while loop runs while the condition is a true statement. You may want code to only run when a variable is false, e.g. while the variable "countingdown" is set to false, but the condition "countingdown == false" has to be a true statement.

Changing while false do to while (CountingDown == false) do is the solution to this particular problem.

2 comments

0

ok i will see if that works

0

Thanks, it worked. I just had to change that and remove the 'break' and it worked

The problem is that you did:

while false do

The problem with that is that while loops will only run if the condition is true, therefore, it won't run the script inside repeatedly. If you wanted to check if counting down was false you could just do:

while not CountingDown do

otherwise, to make a loop run infinitely you'd do:

while true do

however, while true do will crash your script if you don't have any waits inside, another alternative is:

while wait() do

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