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

How to Make A Script That Teleport All Players To Different Positions?

Like In Hunger Games

Pos1 = Vector3.new(-48, 4.5, -108.6)
Pos2 = Vector3.new(-49, 4.5, -108.6)
Pos3 = Vector3.new(-50, 4.5, -108.6)
Pos4 = Vector3.new(-8, 4.5, -108.6)
Pos5 = Vector3.new(-18, 4.5, -108.6)
for i,v in pairs(game.Players:GetPlayers())do
game.Players:GetPlayers()[i].Character:MoveTo(Pos1,Pos2,Pos3,Pos4,Pos5)
end
end

1 comment

0

Teleport All Players To Different Positions Each One

3 answers

Let me start off by thanking you for at least attempting to make it.

Next, you should put all your positions into a table:

pos = {-48, -49, -50, -8, -18} -- I'm just changing the x value since you don't look like you need to change the others.

Now with this we can iterate, or in other words move through, the table along with the players to assign them each their own spots.

Here you can see the iteration:

for _,v in pairs(game.Players:GetPlayers()) do
	v.Character:MoveTo(Vector3.new(pos[_], 4.5, -108.6));
end

I changed a couple things around with your script. One was getting the player. When you use in pairs to get the players, you do not need to get another table of the players a second time. The v represents the player that it is currently on so you can simply use that.

Next is that the :MoveTo method uses a single Vector3 value. So you can not put the multiple values like you did.

If you have any questions, please comment.

What? What does this means? You can't pass a loop value in another loop value, that's the "pairs" and "ipairs" rules, and those floating pos1-5 variables? Not even in a table? And what about this: .Character:MoveTo(Pos1,Pos2,Pos3,Pos4,Pos5) o.o

What you need to do is, first make a table which contains every position, define it like this

Pos1 = Vector3.new(-48, 4.5, -108.6)
Pos2 = Vector3.new(-49, 4.5, -108.6)
Pos3 = Vector3.new(-50, 4.5, -108.6)
Pos4 = Vector3.new(-8, 4.5, -108.6)
Pos5 = Vector3.new(-18, 4.5, -108.6)
local spawns = {Pos1, Pos2, Pos3, Pos4, Pos5}

Now you need to create the function, I don't know where to be called because I don't know your intentions, that's all your own:

function spawnPlayers()
	local passedPlayers = 0
	for _, player in pairs(game.Players:GetChildren()) do
		-- This is the main rule: 1 player = 1 spawn, this means that each player has 1 spawn, so if there are 5 spawns, the max recommended player would be 5 or they will overlap each one
		player.Position = #spawns - passedPlayer
		passedPlayers = passedPlayers + 1 -- I'm out of idea, I'm tired
	end
end

That should work, this means that each player will be spawned in each spawn, as the rule says: 1 player = 1 spawn =)

Ps: I'm tired, the passedPlayers part may not work!

2 comments

0

This won't work. All the players will be at the last spawn location.

0

I saw, I'm tired that's why, I'm editing it now

Here is how I'd write this (commented for explanation):

-- This function takes in a list of players and positions to teleport them to
-- If #players > #positions, the positions are looped around
-- This function was used so that it's easy to reuse
local function teleportPlayersToPositionsInOrder(players, positions)
	-- Keep track of where we are in the positions table
	local positionIndex = 1;

	-- Iterate through each player
	for playerIndex = 1, #players, 1 do
		local player = players[playerIndex];

		-- Require that the player exists
		if (player and player.Character and player.Character:findFirstChild("Torso")) then
			-- Teleport the player to the position
			player.Character:MoveTo(positions[positionIndex]);

			-- Go to the next position using modulo to loop around
			positionIndex = (positionIndex % #positions) + 1;
		end
	end
end



-- The list of positions to teleport to
-- It would be more efficient to store these as just a multidimensional array, rather than Vector3 objects
local positions = {
	Vector3.new(-48, 4.5, -108.6),
	Vector3.new(-49, 4.5, -108.6),
	Vector3.new(-50, 4.5, -108.6),
	Vector3.new(-8, 4.5, -108.6),
	Vector3.new(-18, 4.5, -108.6)
};

-- Call the function with all the players in the game
teleportPlayersToPositionsInOrder(game.Players:GetPlayers(), positions);

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