I wish to improve my teleporter pad script, any help is appreciated

TELEPORT TEST.rbxl (24.7 KB)

Provide an overview of:

  • What does the code do and what are you not satisfied with?
    Well, the code is basically a teleporter pad which is meant to teleport 6 players to a place within the game using Reverse Server and TeleportToPrivateServer, I am not satisfied with the way I’ve done the kicking part of the script when the teleport failed or the way I basically handled laggy players, I think a lot of the code can be improved with your help.
  • What potential improvements have you considered?
    I tried a few methods for this such as waiting until all of the players have teleported but obviously encounted a few problems doing that such as long wait time intervals etc.
  • How (specifically) do you want to improve the code?
    I’d like to basically replace the kicking part of the script with a better system that will handle laggy players and improve the way the teleporter works.

CODE:

local Players = script.Players:GetChildren()
ArrayOfPlayers = {}
local Numbers = 0
local Active = true
local Events = game:GetService("ReplicatedStorage"):WaitForChild("Events")
local TweenService = game:GetService("TweenService")
game.Players.PlayerRemoving:Connect(function(PlayerThatLeft)
	for i,v in pairs(script.Players:GetChildren()) do
		if PlayerThatLeft.Name == v.Name then
			v:Destroy()
			Numbers = Numbers - 1
		end
	end
end)
while wait() do
	--print(unpack(script.Players:GetChildren()))
	local TString = tostring(script.Number.Value)
	script.Parent.BillboardGui.TextLabel.Text = Numbers.."/"..TString
	if Numbers == script.Number.Value then
		Active = false
		local success, errorMessage = pcall(function()
			Code = game:GetService("TeleportService"):ReserveServer(3889235107)
		end)
		if success then
			script.Parent.BillboardGui.TextLabel.TextScaled = true
			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)
					Events:FindFirstChild("TeleportingEvent"):FireClient(v, "Activate")
				end
			end
			local success2, errorMessage2 = pcall(function()
				return game:GetService("TeleportService"):TeleportToPrivateServer(3889235107,Code,ArrayOfPlayers)
			end)
			if success2 then
				for i = 1,200 do 
					local Tabla = script.Players:GetChildren()
					if #Tabla == 0 then
						script.TablaLoop.Value = true						
						break
					end
					wait(0.1)
				end
				if script.TablaLoop.Value == true then
					script.TablaLoop.Value = false
					script.Parent.BillboardGui.TextLabel.Text = Numbers.."/"..TString	
					Active = true	
					ArrayOfPlayers = {}				
				else
					local function KickPlayers()
						local NewPlayerTable = {}
						for i,v in pairs(script.Players:GetChildren()) do
							local PossibleP = game.Players:FindFirstChild(v.Name)
							if PossibleP ~= nil then
								table.insert(NewPlayerTable,PossibleP)
							end
						end
						for i = 1,#NewPlayerTable do
							if NewPlayerTable[i] ~= nil then
								print("TeleportingFailed(1)".." "..NewPlayerTable[i].Name)
								NewPlayerTable[i]:Kick("TELEPORT FAILED!")
								local http = game:GetService("HttpService")
								local date = os.date("!*t")
								local Data = {
									["content"] = NewPlayerTable[i].Name.." got kicked due to a teleportation error ".."(1)"
								}
								Data = http:JSONEncode(Data)
								http:PostAsync("", Data)
							end	
						end
					end
					delay(0,KickPlayers)
					script.Parent.BillboardGui.TextLabel.Text = Numbers.."/"..TString				
					Active = true	
					ArrayOfPlayers = {}								
				end
			elseif errorMessage2 then
				print(errorMessage2)
				local function KickPlayers()
					local NewPlayerTable = {}
					for i,v in pairs(script.Players:GetChildren()) do
						local PossibleP = game.Players:FindFirstChild(v.Name)
						if PossibleP ~= nil then
							table.insert(NewPlayerTable,PossibleP)
						end
					end
					for i = 1,#NewPlayerTable do
						if NewPlayerTable[i] ~= nil then
							print("TeleportingFailed(2)".." "..NewPlayerTable[i].Name)
							NewPlayerTable[i]:Kick("TELEPORT FAILED!")
							local http = game:GetService("HttpService")
							local date = os.date("!*t")
							local Data = {
								["content"] = NewPlayerTable[i].Name.." got kicked due to a teleportation error ".."(2)"
							}
							Data = http:JSONEncode(Data)
							http:PostAsync("", Data)				
						end	
					end
				end
				delay(0,KickPlayers)
				script.Parent.BillboardGui.TextLabel.Text = Numbers.."/"..TString				
				Active = true
				ArrayOfPlayers = {}
			end			
		else
			print(errorMessage)
			local function KickPlayers()
				local NewPlayerTable = {}
				for i,v in pairs(script.Players:GetChildren()) do
					local PossibleP = game.Players:FindFirstChild(v.Name)
					if PossibleP ~= nil then
						table.insert(NewPlayerTable,PossibleP)
					end
				end
				for i = 1,#NewPlayerTable do
					if NewPlayerTable[i] ~= nil then
						print("TeleportingFailed(3)".." "..NewPlayerTable[i].Name)
						NewPlayerTable[i]:Kick("TELEPORT FAILED!")
						local http = game:GetService("HttpService")
						local date = os.date("!*t")
						local Data = {
							["content"] = NewPlayerTable[i].Name.." got kicked due to a teleportation error ".."(3)"
						}
						Data = http:JSONEncode(Data)
						http:PostAsync("", Data)					
					end	
				end
			end
			delay(0,KickPlayers)
			script.Parent.BillboardGui.TextLabel.Text = Numbers.."/"..TString				
			Active = true
			ArrayOfPlayers = {}
		end
	end
	if Active == true then
		for i,v in pairs(game:GetService("Players"):GetPlayers()) do
			if v.Character then
				local HRP = v.Character:FindFirstChild("HumanoidRootPart")
				if HRP then
					if (HRP.Position - script.Parent.Position).Magnitude <= script.Range.Value then
						local Value = false
						for i,Player in pairs(Players) do
							if Player.Name == v.Name then
								Value = true
							end
						end
						if v.Character then
							local Hum = v.Character:FindFirstChild("Humanoid")
							if Hum then
								local CurrentNum = script.CurrentNum
								CurrentNum.Value = 0	
								for i,v in pairs(script.Players:GetChildren()) do
									CurrentNum.Value = CurrentNum.Value + 1
								end
								if Value == false and Hum.Health > 0 and script.CurrentNum.Value <= script.Number.Value - 1 then
									local PlayerValue = Instance.new("BoolValue")
									if not script.Players:FindFirstChild(v.Name) then
										PlayerValue.Name = v.Name
										PlayerValue.Parent = script.Players
										Numbers = Numbers + 1
									end
								end
							end
						end
					end
				end
			end
			if v.Character then
				local HRP = v.Character:FindFirstChild("HumanoidRootPart")
				local Hum = v.Character:FindFirstChild("Humanoid")
				if HRP and Hum then
					if ((HRP.Position - script.Parent.Position).Magnitude > script.Range.Value or Hum.Health <= 0) then
						for i,Player in pairs(script.Players:GetChildren()) do
							if Player.Name == v.Name then
								Player:Destroy()
								Numbers = Numbers - 1
							end
						end
					end
				end
			end
		end
	end
end
2 Likes

can you paste the code instead? Some people either don’t trust downloading files or doesn’t want to.

1 Like

Since it’s an rbxl file I don’t see what’s the problem but I’ll try, it’s long.

Yes, I don’t really know how to send the code in a way it’ll be readable how do I do that?

you can click the button to the left to the upload button.

Frst paste your script, then select the whole script, then click the button

There I’ve added the code, removed the webhook part so ppl won’t spam my channels uh, that script is placed inside of the pad.

You can get the players ping, and adjust accordingly.

Is that really the problem though? I’ve been getting ton of teleport errors some for even all of the 6 players and the wait time till the kick is 20 seconds… I think the way I did the check for the first kicking method “(1)” is wrong and inefficient but idk how can I improve it.

I don’t know much about teleport service, but i’ve heard it’s prettty bad.

1 Like

You’re right about that one lol, but in this case I just think my script is inefficient.

What are the teleport errors? I’ll look into it.

Basically the first kicking method is very common to execute, so it’s not the teleport service in my opinion, it is probably the way I did the “tabla” check thing.

@SalemxDxD Hi again, did you find anything that might help?

@VegetationBush I’ve done the changes you’ve requested do you mind looking into my problem, no one else seems to and I really don’t know what to do at this point.

Sorry for the late reply(I forgot sorry)

You can modify this a bit:

game.Players.PlayerRemoving:Connect(function(PlayerThatLeft)
	for i,v in pairs(script.Players:GetChildren()) do
		if PlayerThatLeft.Name == v.Name then
			v:Destroy()
			Numbers = Numbers - 1
		end
	end
end)

to this

game.Players.PlayerRemoving:Connect(function(PlayerThatLeft)
	PlayerThatLeft:Destroy()
	Numbers = Numbers - 1
end)

because PlayerThatLeft is already defined, no need to loop all the players.

The players thingy is a folder which in it are values which are named after the players on the Pad so I had to do that loop to get the correct value out

Then you can simply do:

script[PlayerThatLeft.Name]:Destroy()

What about the kicking method and the check i did with the for loop to see if there are still people on the pad after 20 seconds?

I don’t really get what you mean, can you elaborate a bit?

“Basically the first kicking method is very common to execute, so it’s not the teleport service in my opinion, it is probably the way I did the “tabla” check thing.” That thing

1 Like