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