I’m making a player limiter teleporter everything worked fine but
I want it to delete the player when the player leaves the game
it worked in Studio but it didn’t work in-game
any help would be great (:
-- SERVICES --
local Players = game:GetService('Players')
local ts = game:GetService('TeleportService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
-- GAME INFO --
local PlaceId = 9555926533
local maxPlayers = 16
local teleport = script.Parent
-- PLAYERS AND NODES --
local Folder = script.Parent:WaitForChild('Players')
-- UI STUFF --
local playerCount = script.Parent:WaitForChild('billboard'):WaitForChild('SurfaceGui'):WaitForChild('TextLabel')
local timerText = script.Parent:WaitForChild('billboard'):WaitForChild('timer'):WaitForChild('TextLabel')
local intermissionLength = 30
local TimerActive = false
local debounce = false
local DetectEnd = {}
local studs = 10
-- FUNCTIONS --
Players.PlayerRemoving:Connect(function(player)
for _,value in pairs(Folder:GetChildren()) do
if value.Value == player.Character then
print('destroy')
value:Destroy()
end
end
end)
function checkIfExists(Character) -- checks if player exists
for _,value in pairs(Folder:GetChildren()) do
if value.Value == Character then return true end
end
return false
end
function teleportParty()
local Plrs = {}
for _,value in pairs(Folder:GetChildren()) do
if not value then continue end
table.insert(Plrs,Players:GetPlayerFromCharacter(value.Value))
end
local Code = ts:ReserveServer(PlaceId)
ts:TeleportToPrivateServer(PlaceId,Code,Plrs)
Folder:ClearAllChildren()
end
function timer()
if TimerActive then return end
TimerActive = true
intermissionLength = 30
while task.wait() do
for i = intermissionLength,0,-1 do
timerText.Visible = true
wait(1)
intermissionLength = i
timerText.Text = i
if #Folder:GetChildren() < 2 then
intermissionLength = 30
timerText.Text = intermissionLength
TimerActive = false
return
end
end
if #Folder:GetChildren() >= maxPlayers or intermissionLength == 0 then
TimerActive = false
teleportParty()
end
end
end
script.Parent.Touched:Connect(function(part)
if not part.Parent:FindFirstChild('Humanoid') then return end
if debounce then return end
debounce = true
local player = part.Parent
if not checkIfExists(player) then
local Tag = Instance.new('ObjectValue')
Tag.Value = player
Tag.Parent = Folder
table.insert(DetectEnd, player.HumanoidRootPart)
end
if #Folder:GetChildren() >= 2 then
timer()
end
debounce = false
end)
while wait() do
table.foreach(DetectEnd, function(index, HumanoidRootPart)
local checkDistance = (HumanoidRootPart.Position - teleport.Position).Magnitude
if checkDistance > studs then
local Player = game.Players:GetPlayerFromCharacter(HumanoidRootPart.Parent)
for _,value in pairs(Folder:GetChildren()) do
if value.Value == Player.Character then
value:Destroy()
end
end
end
end)
end
this line works in Studio but doesn’t work in-game like in Roblox
Players.PlayerRemoving:Connect(function(player)
for _,value in pairs(Folder:GetChildren()) do
if value.Value == player.Character then
print('destroy')
value:Destroy()
end
end
end)
I made sure I published the game multiple times