Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by 4 years ago

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.

0
Is there a reason you can't use Player.Character.HumanoidRootPart.CFrame? NoelGamer06 62 — 4y
0
Would that fix the situation I’m in? mrfrank79 30 — 4y
0
No that wouldn't. Just use SetPrimaryPartCFrame. Dovydas1118 1495 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
I’ll check it out when i get the time, if it works correctly, I’ll accept the answer. Thanks! mrfrank79 30 — 4y
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. mrfrank79 30 — 4y
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. mrfrank79 30 — 4y
0
You need to use a CFrame for SetPrimaryPartCFrame() instead of a Vector3. Dovydas1118 1495 — 4y
Ad

Answer this question