Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my teleportation script only work for one player instead of them all?

my script works only for one player. it will not teleport the rest. can anyone explain why?

i am using a script in the serverscripts part, i want all players to teleport to a certain position but only one player gets teleported when i test it.

for i, player in pairs(plrs) do
		if player then
			character = player.Character
			
			if character then
				--Teleport 
				
				local gamepad = game.Workspace.GameParts.GamePad
				
				character:FindFirstChild("HumanoidRootPart").CFrame = gamepad.CFrame
				
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
				
			else 
				--There is no character
				if not player then
					print("removed"..i)
					table.remove(plrs, i)
				end
			end
		end

4 comments

0

Context is important. Why do you want to teleport everybody - What triggers the players to teleport - Are you using a ServerScript/LocalScript

0

i've updated it, i hope that helps

0

You intended it to teleport all the players when you touched the part or some gui or chat command?

0

It's a round game, so it's intended to teleport players into the round automatically when a new round is starting up

3 answers

I'll lay out the basics for you.

Firstly, we want to create local variables. These variables will make things much cleaner and therefore easier to work with.

local Players = game:GetService("Players") - Getting the Players service local TeleportPad = workspace.GameParts.GamePad - Pointing to the part that we want everybody to teleport to

The next step will be to create a loop so we can go through all the players in the game and do something to them.

for _, player in pairs(Players:GetChildren()) do - The important part to note is the Players:GetChildren() which creates a table of all the Players in the game for us to loop through.

Now for the loop itself. We now want to find the corresponding players character by doing local char = player.Character or player.CharacterAdded:Wait() This will either immediately find the players character or (if it cannot find the player) wait until the player has spawned in, after which, get the HumanoidRootPart - the part we will be teleporting. local torso = char:FindFirstChild("HumanoidRootPart")

Finally, we check to see whether the HumanoidRootPart actually exists. If it does, the player will be teleported to the position mentioned at the start.

if torso then
	torso.Position = TeleportPad.Position
end

The full code:

local Players = game:GetService("Players")
local TeleportPad = workspace.GameParts.GamePad

for _, player in pairs(Players:GetChildren()) do
	local char = player.Character or player.CharacterAdded:Wait()
	local torso = char:FindFirstChild("HumanoidRootPart")
	if torso then
		torso.Position = TeleportPad.Position
	end
end

3 comments

0

Please mark as answered if I have helped

0

This is just a teleport script, the script that I am using works but it doesn't work for everyone, that is my problem

0

The script will work in your case if you use the logic within the loop. It will only teleport the players who are actually in workspace.

Try this:

for i, player in pairs(plrs) do --the variable "plrs" should already be set to the list of players in the server.
	player.Character.HumanoidRootPart.Position = game.Workspace.GameParts.GamePad.Position --add a Vector.new(0, 1, 0) if that is too low to make them teleport a bit above it
end

You can get a list of players in the server with game.Players:GetPlayers(), which returns a table that you can then put into here: for i, player in pairs(game.Players:GetPlayers()), or just assign game.Players:GetPlayers() to your variable "plrs"

Try this:

Assuming the gamepad part is in the workspace, insert the following:

local GamePad = workspace.GamePad

for _,player in pairs(game.Players:GetPlayers()) do
	player.Character.HumanoidRootPart.CFrame = GamePad.CFrame
end

I suggest using game.Players:GetPlayers() on every loop, so it updates every time.

1 comment

0

CFrame is the Coordinate Frame.

This discussion is preserved as it was. New answers, comments, and votes are closed.