so i made a script that adds person into a table if he touches a part, but how do i make it so, it removes him from a table if he stops touching a part?
local TouchBrick = script.Parent
local PlayersToTeleport = {}
local function addPlayerToTable(TouchedPart)
if TouchedPart.Parent:FindFirstChild("Humanoid") then
local Character = TouchedPart.Parent
local blueTP = workspace.BlueTeleporter
local Player = game.Players:GetPlayerFromCharacter(Character)
local AlreadyInTable = false
for _,OtherPlayer in next,PlayersToTeleport do
if OtherPlayer == Player then
AlreadyInTable = true
end
end
if not AlreadyInTable then
table.insert(PlayersToTeleport,Player)
wait(5) -- remove it later
local blue = PlayersToTeleport[math.random(1, #PlayersToTeleport)]
blue.Character:MoveTo(blueTP.Position)
end
end
end
TouchBrick.Touched:Connect(addPlayerToTable)
Assuming you want to remove player from table if he stops touching a part for more than 5 seconds, you can do it something like this: local TouchBrick = script.Parent
local PlayersToTeleport = {}
local function addPlayerToTable(TouchedPart)
if TouchedPart.Parent:FindFirstChild(“Humanoid”) then
local Character = TouchedPart.Parent
local blueTP = workspace.BlueTeleporter
local Player = game.Players:GetPlayerFromCharacter(Character)
local AlreadyInTable = false
for _,OtherPlayer in next,PlayersToTeleport do
if OtherPlayer == Player then
AlreadyInTable = true
end
end
if not AlreadyInTable then
table.insert(PlayersToTeleport,Player)
wait(5)
local blue = PlayersToTeleport[math.random(1, #PlayersToTeleport)]
blue.Character:MoveTo(blueTP.Position)
end
end
end
TouchBrick.Touched:Connect(addPlayerToTable)
local function onPartTouchEnd(partTouchEnd)
local TouchedPlayer = partTouchEnd.Parent
local TouchedPlayerName = TouchedPlayer.Name
for i,v in pairs(PlayersToTeleport) do
if TouchedPlayerName == v.Name then
for j = i, #PlayersToTeleport do
PlayersToTeleport[j] = PlayersToTeleport[j+1]
end
PlayersToTeleport[#PlayersToTeleport] = nil
break
end
end
end