# Player In Pairs Loop Not Working Correctly?

Hi, I have a script that teleports each player to a random location. For some reason, they are all spawning at the same location. Here is my script:

``````			if plr.Character ~= nil then
local num = math.random(math.floor(1,13))
local pos = nil
if num == 1 then
pos = Vector3.new(35,5,35)
end
if num == 2 then
pos = Vector3.new(35,5,-35)
end
if num == 3 then
pos = Vector3.new(-35,5,35)
end
if num == 4 then
pos = Vector3.new(-35,5,-35)
end
if num == 5 then
pos = Vector3.new(65,5,65)
end
if num == 6 then
pos = Vector3.new(65,5,-65)
end
if num == 7 then
pos = Vector3.new(-65,5,65)
end
if num == 8 then
pos = Vector3.new(-65,5,-65)
end
if num == 9 then
pos = Vector3.new(-95,5,-95)
end
if num == 10 then
pos = Vector3.new(-95,5,95)
end
if num == 11 then
pos = Vector3.new(95,5,-95)
end
if num == 12 then
pos = Vector3.new(95,5,95)
end
if num == 13 then
pos = Vector3.new(0,5,0)
end
plr.Character.HumanoidRootPart.Position = pos
end
end``````

Make it:

``````local num = math.random(1, 13)
``````

It might be becuase your math.random is not in a loop, along with that math.random only creates a whole number value, you shouldnâ€™t use if statements like that and have code smell Code smell - Wikipedia instead make a table containing all vector 3 values then set position as tableOfSpawnValues[num]

Heres what the code should look like, obv not perfect or syntax checked

``````local playerLocations = {
{blah,blah,blah}
{blah,blah,blah}
{blah,blah,blah}
{blah,blah,blah}
{blah,blah,blah}
{blah,blah,blah}
{blah,blah,blah}
}

if not plr.Characer ~= nil then
While true do -- or whatever loop your puting it, probably should be a for loop getting children of players
local num = math.random(1,13)
local finalPos = playerLocations[num]
local pos = Vector.new(finalPos)
end
end

``````

I found two issues with your code, the main one was `local num = math.random(math.floor(1, 13))` which was being translated to `math.random(1)` â†’ `math.random(1, 1)` â†’ `1` everytime. The second issue was that Instead of the HumanoidRootPart `CFrame` you where setting the `Position` which wont move the entire model. I fixed those issues and tried cleaning your code:

``````local Players = game:GetService("Players")

--you can add or remove as many vectors you like!
local positions = {
Vector3.new(35,5,35),
Vector3.new(35,5,-35),
Vector3.new(-35,5,35),
Vector3.new(-35,5,-35),
Vector3.new(65,5,65),
Vector3.new(65,5,-65),
Vector3.new(-65,5,65),
Vector3.new(-65,5,-65),
Vector3.new(-95,5,-95),
Vector3.new(-95,5,95),
Vector3.new(95,5,-95),
Vector3.new(95,5,95),
Vector3.new(0,5,0)
}

for i, plr in pairs(Players:GetPlayers()) do
local char = plr.Character
if not char then continue end --continue skips the current iteration
--when the arguements of math.random start from 1, it can be skipped
local num = math.random(#positions)
local pos = positions[num]
local root = char:WaitForChild("HumanoidRootPart", 5)
if not root then continue end
--instead of Position, you have to use CFrame to teleport their PrimaryPart to move the entire model
--The PrimaryPart of char is their HumanoidRootPart(by default)
root.CFrame = CFrame.new(pos)
end
``````

A minor flaw with your script. Itâ€™s important to make sure the PrimaryPart is set and exists before using SetPrimaryPartCFrame to avoid it from erroring and the loop breaking.

1 Like

Fixed it! Thanks for pointing out.

It is in a `for _, plr in pairs(game.Players:GetChildren()) do` loop. Sorry for not including that in original post.

I think thatâ€™s the issue. So I beleive `math.floor(math.random(1,13))` would work, correct?

Im pretty sure you can bassicly just copy past the script that PAGO made and tahts you solution
His code also sohuld allow you to remove and add positions with ease

Why not randomly generate the vectors? Couldnâ€™t you just randomly generate each part of the vector, like this:

*PS, I would recommend the built-in `Random` library rather than `math.random`

``````local rand = Random.new()

--Getting the spawn pos:
local spawnPos = Vector3.new(rand:NextNumber(-35,65),5,rand:NextNumber(-65,65))

--Move the player to that place. MoveTo() might work well in this case
--Since it assures things to move inside of eachother.
``````

Try:

``````plr.Character.HumanoidRootPart.CFrame = pos.CFrame
``````