Why is the kicking part firing way too many times even though it's not supposed to?

I wish to understand why is there so many kicking cases even though I give 15 seconds till they get kicked, they should be teleported by then? My logs are reporting ton of the “x got kicked due to a teleportation error (TESTING) (1)”

Players = script:WaitForChild("Players")
Busy = script:WaitForChild("Busy")
PlaceId = script:WaitForChild("PlaceId")
PlayersNeeded = script:WaitForChild("PlayersNeeded")
Range = script:WaitForChild("Range")
LoopStop = script:WaitForChild("LoopStop")
Pad = script.Parent
Events = game.ReplicatedStorage:WaitForChild("Events")

game.Players.PlayerRemoving:Connect(function(PlayerWhoLeft)
	if Players:FindFirstChild(PlayerWhoLeft.Name) then
		Players:FindFirstChild(PlayerWhoLeft.Name):Destroy()
	end
end)

while wait() do	
	if Busy.Value == false then
		script.Parent.BillboardGui.TextLabel.Text = tostring(#Players:GetChildren()).."/"..tostring(PlayersNeeded.Value)
		for i,v in pairs(game.Workspace:GetDescendants()) do
			if v.Name == "HumanoidRootPart" then
				local Magnitude = (v.Position - Pad.Position).Magnitude
				if Magnitude <= 13 then
					if #Players:GetChildren() < PlayersNeeded.Value then
						local Player = game.Players:GetPlayerFromCharacter(v.Parent)
						if Player ~= nil then
							if not Players:FindFirstChild(Player.Name) then
								local s = Instance.new("BoolValue")
								s.Name = Player.Name
								s.Parent = Players
							end
						end
					elseif #Players:GetChildren() == PlayersNeeded.Value then
						Busy.Value = true
						local function SetBusyToFalse()
							repeat wait() until #Players:GetChildren() == 0
							Busy.Value = false
						end
						delay(0,SetBusyToFalse)
					end
				else
					local Player = game.Players:GetPlayerFromCharacter(v.Parent)
					if Player ~= nil then
						if Players:FindFirstChild(Player.Name) then
							Players:FindFirstChild(Player.Name):Destroy()
						end						
					end					
				end
			end
		end
	else
		if LoopStop.Value == false then
			LoopStop.Value = true
			local success, errorMessage = pcall(function()
				Code = game:GetService("TeleportService"):ReserveServer(PlaceId.Value)
			end)
			if success then
				local ArrayOfPlayers = {}
				script.Parent.BillboardGui.TextLabel.Text = "TELEPORTING!"
				for i,v in pairs(game.Players:GetChildren()) do
					if script.Players:FindFirstChild(v.Name) then
						table.insert(ArrayOfPlayers,v)
						v.Backpack.LocalValues.Teleporting.Value = true
						Events:FindFirstChild("TeleportingEvent"):FireClient(v, "Activate")
					end
				end
				local success2, errorMessage2 = pcall(function()
					return game:GetService("TeleportService"):TeleportToPrivateServer(PlaceId.Value,Code,ArrayOfPlayers)
				end)
				if success2 then
					for i,v in pairs(ArrayOfPlayers) do
						local function Kick(PlayerX)
							if game.Players:FindFirstChild(PlayerX.Name) and workspace:FindFirstChild(PlayerX.Name) and PlayerX.Backpack.LocalValues.Teleporting.Value == true then
								PlayerX:Kick("TELEPORT FAILED!")
								local http = game:GetService("HttpService")
								local date = os.date("!*t")
								local Data = {
									["content"] = PlayerX.Name.." got kicked due to a teleportation error (TESTING) ".."(1)"
								}
								Data = http:JSONEncode(Data)
								--http:PostAsync("", Data)
							end
						end
						local function DelayThat()
							Kick(v)
						end
						delay(15,DelayThat)
					end
					repeat wait() until #Players:GetChildren() == 0
					LoopStop.Value = false
				elseif errorMessage2 then
					print(errorMessage2)
					for i,v in pairs(ArrayOfPlayers) do
						local function Kick(PlayerX)
							if game.Players:FindFirstChild(PlayerX.Name) and workspace:FindFirstChild(PlayerX.Name) and PlayerX.Backpack.LocalValues.Teleporting.Value == true then
								PlayerX:Kick("TELEPORT FAILED!")
								local http = game:GetService("HttpService")
								local date = os.date("!*t")
								local Data = {
									["content"] = PlayerX.Name.." got kicked due to a teleportation error (TESTING) ".."(2)"
								}
								Data = http:JSONEncode(Data)
								--http:PostAsync("", Data)	
							end
						end
						local function DelayThat()
							Kick(v)
						end
						delay(0,DelayThat)
					end
					repeat wait() until #Players:GetChildren() == 0 
					LoopStop.Value = false
				end
			elseif errorMessage then
				print(errorMessage)
				local ArrayOfPlayers = {}
				for i,v in pairs(game.Players:GetChildren()) do
					if script.Players:FindFirstChild(v.Name) then
						table.insert(ArrayOfPlayers,v)
					end
				end				
				for i,v in pairs(ArrayOfPlayers) do
					local function Kick(PlayerX)
						if game.Players:FindFirstChild(PlayerX.Name) and workspace:FindFirstChild(PlayerX.Name) and PlayerX.Backpack.LocalValues.Teleporting.Value == true then
							PlayerX:Kick("TELEPORT FAILED!")
							local http = game:GetService("HttpService")
							local date = os.date("!*t")
							local Data = {
								["content"] = PlayerX.Name.." got kicked due to a teleportation error (TESTING) ".."(3)"
							}
							Data = http:JSONEncode(Data)
							--http:PostAsync("", Data)					
						end
					end
					local function DelayThat()
						Kick(v)
					end
					delay(0,DelayThat)
				end
				repeat wait() until #Players:GetChildren() == 0 
				LoopStop.Value = false
			end
		end
	end
end

Please include the minimum amount of code in the question needed to illustrate your problem.

In future, please follow the format so we can help you efficiently, and please only include code necessary for us to help you.

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? Include screenshots / videos if possible!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?)

I would but I myself don’t know why is this happening so I might be taking down some important code…

There is way too much code here and I do not want to read your entire script to try and help. If I had to try and isolate a cause though, a glaring problem is that your entire code is wrapped in a while loop - a badly written one.

You’re going to need to do a lot of revision and decoupling in your system because the root of your issue is caused in the way that this system is being handled fundamentally. Redoing the code isn’t a bad option either.

Consider using a more functional or event-based system to accomplish this, so either BindableEvents or ModuleScripts. Divide and conquer: have several processes moved out of the loop (which is fairly expensive per iteration) and into events that can be fired off.

The loop should only handle any countdowns, live updating of player counts and a player’s distance to the teleport circle. Everything else can be moved out into its own separate process. This would help debugging, organising and future updates of your code massively better to deal with.

Just a side note: probably don’t want to kick users for failed teleports, which is indicative of bad UX. Player experience should be interrupted as little as possible and the engine already alerts for failed teleports. You can just account for this case by undoing teleport procedures on the player (hide TeleportGuis, make use of TeleportInitFailed, so on) and letting them continue with gameplay again.

2 Likes

Thanks for the advices I get the code is super long and I’ll make it efficient as you’ve said but there is 1 important thing that’s bothering me about the code and I dont think I’ll know how to fix it without your help that’s the first part which im kicking players at, which I directed people to in my post, I know it’s long but I simply don’t know where to cut it, I’m afraid I’d cut by mistake the cause for the issue, I just want to know why am I getting so many kick cases (I won’t kick players after I edit the code but still I want to know why is this happening there shouldnt be so many laggy players, usually teleporting takes around 5 seconds and not 15 whole seconds)

It might be happening because wait() might not be enough time for the player to be kicked, so it manages to run through the code again and kicks the player while they’re getting kicked. Try increasing the wait time to see if that works.

I’ll let you know tomorrow if it worked, I’ve increased it to 0.25

  1. Add a wait(.2)
    or you could try
  2. Debounce.