Hello, I have a script from @YT_Forceman, and I modified the script a little bit, and ended up with this script, but the player doesn’t teleport. Is there any problem with the teleporting script? Thank you.
local lobby = game.Workspace.LobbyTeleport
local inRound = game.ReplicatedStorage.InRound
local PlayersInPartRegion = {game.Workspace.BattleZoneAreaPart}
game.ReplicatedStorage.AddPlayerToTable.OnServerEvent:Connect(function(PlayerToAdd)
table.insert(PlayersInPartRegion,PlayerToAdd.Name)
end)
game.ReplicatedStorage.RemovePlayerFromTable.OnServerEvent:Connect(function(PlayerToRem)
local Count = 0
for _,index in pairs(PlayersInPartRegion) do
Count = Count + 1
if index == PlayerToRem.Name then
table.remove(PlayersInPartRegion,Count)
break
end
end
end)
if #PlayersInPartRegion < 1 then
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char.HumanoidRootPart.CFrame = lobby.CFrame
end
end
Looking back on a post he made earlier, I assume that he wants to teleport all players back to the lobby when there is only 1 player left in the arena…
The player does not teleport because the if statement is only running once (when there is 1 values in PlayersInPartRegion, being BattleZoneAreaPart which should not be there). Depending on what you are going for, you might just want to loop through the PlayersInPartRegion array every few seconds and then teleport whoever is there.
You are also looping through the array of all players in the game, not players in the array. You should be adding the player objects to the array, not names. I would recommend setting PlayersInPartRegion to a weak table like this local PlayersInPartRegion = setmetatable({}, {__mode = "v"}) to clean up any players who have left the game automatically once they have been added to the array.
Unrelated to the topic, but you can use table.find(array, value) to grab the index of a value in an array over using a loop in a way this like this table.remove(PlayersInPartRegion, table.find(PlayersInPartRegion, PlayerToRem.Name)).
That won’t work, I don’t think, because that is saying there will be no players in the area, which I don’t think he wants to do… Sorry if that is what he wants
EDIT: Also, when the loop breaks when there isn’t the correct number of players, it won’t check anymore. Just have
while wait(1) do
if #PlayersInPartRegion = 1 then
-- Do stuff here
end
end
@superMathMan9 I changed the script, but it’s still not teleporting players.
Here is the script:
if #PlayersInPartRegion == 1 then
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char:MoveTo(lobby.Position)
end
end
That looks better, but you need to put it in a loop. Hope this helps
while wait(1) do
if #PlayersInPartRegion == 1 then
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char:MoveTo(lobby.Position)
end
end
end
if #PlayersInPartRegion < 1 then
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
if char then
char:SetPrimaryPartCFrame(lobby.CFrame) end
end
end
You need to Also Check And Teleport the Players who Are Touching The Part.
You do Not want to Teleport the Players who are NOT Touching the Part.
This is The Correct Script.
while wait(1) do
if #PlayersInPartRegion == 1 then
for _, player in pairs(game.Players:GetChildren()) do
for i,Index in pairs(PlayersInPartRegion) do
if Index == player.Name then -- Found Player In Table
local char = player.Character
char:MoveTo(lobby.Position)
end
end
end
end