Player doesn't teleport

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

Are you getting any errors? or is it just not teleporting

Does it have something to do with the fact that it says < 1? Wouldn’t that mean that no players are there to teleport?

EDIT: I do believe this is the problem. :slight_smile:

1 Like

Seems like it is to my knowledge as well. I think he expects it to be less than 1 but it never is so it just doesn’t teleport

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)).

3 Likes

Change this to:

char:SetPrimaryPartCFrame(lobby.CFrame)

as it respects joints.

Also,

1 Like

There are no errors, the players are not teleporting.

Again, put the if statement in a loop and make it say #PlayersInPartRegion = 1

1 Like

Try to use a loop for it, while #PlayersInParts < 1 do

1 Like

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 :stuck_out_tongue:

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
2 Likes

I changed char.Humanoid.CFrame = lobby.CFrame into char:MoveTo(lobby.Position).

I don’t expect that will work because it is a model, you can just use char:SetPrimaryPartCFrame(CFrame) if you want

1 Like

@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
1 Like

I have one problem, it keeps on teleporting players to the lobby’s position.

Make sure to update the list of players touching the part or it will continue to think that there is only 1 player touching it.

1 Like
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
1 Like

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
1 Like

The script keeps teleporting players instead of teleporting them one time.