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

Can't teleport players to different parts in a folder?

Here's the code:

	local spawns = chosenMap.Spawns:GetChildren()
	for _, Player in next, game:GetService("Players"):GetPlayers() do
		local SelectedPart = spawns[math.random(1, #spawns)]
	    Player.Character:MoveTo(SelectedPart.Position)
	end

When I test the game with multiple players, everyone gets teleported to the same part, there are four possible part teleportations in total. Help would be appreciated.

3 comments

0

Is there a reason you can't use Player.Character.HumanoidRootPart.CFrame?

0

Would that fix the situation I’m in?

0

No that wouldn't. Just use SetPrimaryPartCFrame.

1 answer

✓ Accepted answer

Right, you're on the right track, but I suggest using an in pairs loop instead of a next for loop. It loops through tables, and it gets the value, and that's what you need. It is recommended you use an in pairs loop as it is more efficient.

local spawns = chosenMap.Spawns:GetChildren()
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
	local selectedPart = spawns[math.random(1, #spawns)]
	player.Character:MoveTo(selectedPart.Position)
end

Also you should use SetPrimaryPartCFrame() to get both position and orientation of the player.

4 comments

0

I’ll check it out when i get the time, if it works correctly, I’ll accept the answer. Thanks!

0

Also i tried using SetPrimaryPartCFrame but I don’t know if i did it properly cause i just got an error. Additionally, is it possible to make this script wait for players first? I get errors on that too and it’s pretty annoying.

0

Is the player even considered a model because we are fetching them from players, I'm unsure if my script is properly laid out.

0

You need to use a CFrame for SetPrimaryPartCFrame() instead of a Vector3.

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