For loop is stuck

Well yea as the title says thats my problem. For Context I am trying to teleport all players in the party to that one place

So this is the server script:


local Plrs = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("Events").Teleport

local Queue = {}

event.OnServerEvent:Connect(function(plr, placeId, amount)
	if table.find(Queue, plr) then return end

	if not table.find(Queue, plr) then
		table.insert(Queue, plr)
	end

	if #Queue <= amount then
		local teleporting = {}
		for i = 1, amount do
			table.insert(teleporting, Queue[i])	--keeps doing this for loop and idk why		
            print("T") -- keeps printing T
			task.wait()
		end

		local reservedServer = TeleportService:ReserveServer(placeId)
		TeleportService:TeleportToPrivateServer(placeId, reservedServer, teleporting)	
		
		for i = 1, #teleporting do
			local index = table.find(Queue, teleporting[i])
			if index then
				table.remove(Queue, index)
				task.wait()
			end
		end
	end
end)

Client:

Party.StartButton.Activated:Connect(function()
			if plr.Name == Party.Players:WaitForChild("PlayerCard1").PlrName.Text then
				for i,v in pairs(Party.Players:GetDescendants()) do
					if v:IsA("TextLabel") and v.Name == "PlrName" then
						if Players:FindFirstChild(v.Text) then						
							ReplicatedStorage:WaitForChild("Events").Teleport:FireServer(v.Text, currentMap.placeId.Value, number)
						end						
					end
				end
			end			
		end)

Tell me if you need more information.

4 Likes

Not really sure why the for loop is running forever.
My guess is the [amount] in your for loop is inf

This can happen if something wacky happens like dividing a number by 0
maybe your [amount] does something like this?

print(1/0) --'inf'
1 Like

well I am getting the number from a text box:

local number = tonumber(CreatePartyFrame.PlayerCount.Text)		
1 Like

I noticed that you didn’t provide the correct arguments for your remote event

--client
Teleport:FireServer(v.Text, currentMap.placeId.Value, number)

--server, plr is always there as they fired the remote event
event.OnServerEvent:Connect(function(plr, "v.Text is missing here" , placeId, amount)
1 Like

its trying to teleport the players now, but I get the error:
Screenshot 2023-09-11 204135

In here:

local Plrs = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("Events").Teleport

local Queue = {}

event.OnServerEvent:Connect(function(player, plr, placeId, amount)
	if table.find(Queue, plr) then return end

	if not table.find(Queue, plr) then
		table.insert(Queue, plr)
	end

	if #Queue <= amount then
		local teleporting = {}
		for i = 1, amount do
			table.insert(teleporting, Queue[i])			
			task.wait()
		end

		local reservedServer = TeleportService:ReserveServer(placeId)
		TeleportService:TeleportToPrivateServer(placeId, reservedServer, teleporting)	--error here
		
		for i = 1, #teleporting do
			local index = table.find(Queue, teleporting[i])
			if index then
				table.remove(Queue, index)
				task.wait()
			end
		end
	end
end)

My favorite thing can help you:

Set a breakpoint and once it is hit, you will be able to open the watch list window and see what arguments were sent.

When setting a breakpoint in clientscript make sure to set it before you start testing.

1 Like

Well it gives me this:


and this doesn’t really help me :sweat_smile:

1 Like

Amount is 4 so it rules out inf.

It must be the event that fires multiple times.

Easy to investigate:

print("T" .. tostring(amount))
1 Like

even when the amount is one it still gives me the error:
Screenshot 2023-09-11 204135

for here:

local Plrs = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("Events").Teleport

local Queue = {}

event.OnServerEvent:Connect(function(player, plr, placeId, amount)
	if table.find(Queue, plr) then return end

	if not table.find(Queue, plr) then
		table.insert(Queue, plr)
	end

	if #Queue <= amount then
		local teleporting = {}
		for i = 1, amount do
			table.insert(teleporting, Queue[i])	
			print("T" .. tostring(amount))
			task.wait()
		end

		local reservedServer = TeleportService:ReserveServer(placeId)
		TeleportService:TeleportToPrivateServer(placeId, reservedServer, teleporting)	
		
		for i = 1, #teleporting do
			local index = table.find(Queue, teleporting[i])
			if index then
				table.remove(Queue, index)
				task.wait()
			end
		end
	end
end)
1 Like

Just a guess

Studio limitation

This service does not work during playtesting in Roblox Studio — To test aspects of your game using it, you must publish the game and play it in the Roblox application.

2 Likes

The error appears when I am playing in the Roblox application. When I am playing in Studio it just says HTTP 403 Forbidden or something like that (even tough I enabled that setting)

ok sorry for all the trouble lol I found the issue.
I was trying to teleport just a string the whole time.
I am so sorry for wasting your time.

2 Likes