Problem: I wanted the script to teleport each player to a random spawn location but, every single player get’s teleported to the same location that is chosen. How can I teleport each player to a different location?
Script:
local MapChosen = Maps[math.random(1, #Maps)]:Clone()
local SpawnLocations = MapChosen:FindFirstChild(‘Spawns’):GetChildren()
local RandomLocation = Spawns[math.random(1, #Spawns)]
for _, Player in pairs(Players:GetChildren())do
if Player.Ready.Value == true then
if Player.Character and Player.Character:FindFirstChild('Humanoid') then
Player.Character.HumanoidRootPart.CFrame = RandomLocation.CFrame
end
end
end
This won’t work unless you have one spawn (AT LEAST) for each player. This isn’t the best way to do this, but oh well here
Feel free to make modifications (I slapped this together lazily lol)
local MapChosen = Maps[math.random(1, #Maps)]:Clone()
local SpawnLocations = MapChosen:FindFirstChild(‘Spawns’):GetChildren()
local function pickspawn()
local RandomLocation = Spawns[math.random(1, #Spawns)]
for i,v in pairs(Spawns) do if v == RandomLocation then table.remove(Spawns,i) end
return RandomLocation
end
for _, Player in pairs(Players:GetChildren())do
local lol = pickspawn()
if Player.Ready.Value == true then
if Player.Character and Player.Character:FindFirstChild('Humanoid') then
Player.Character.HumanoidRootPart.CFrame = lol.CFrame
end
end
end
For each player to be teleported to a different spawn you would need to randomize the spawn inside the loop, if you put it outside the for loop like in your code, the game would just pick a random spawn, then teleport everyone to that single spawn instead of picking a random spawn for each player
I might be wrong, but you would simply put local RandomLocation = Spawns[math.random(1, #Spawns)] inside the loop.
local MapChosen = Maps[math.random(1, #Maps)]:Clone()
local SpawnLocations = MapChosen:FindFirstChild('Spawns'):GetChildren()
for _, Player in pairs(Players:GetChildren())do
local RandomLocation = Spawns[math.random(1, #Spawns)]
if Player.Ready.Value == true then
if Player.Character and Player.Character:FindFirstChild('Humanoid') then
Player.Character.HumanoidRootPart.CFrame = RandomLocation.CFrame
end
end
end
for _, Player in pairs(Players:GetChildren())do
if Player.Ready.Value == true then
local RandomLocation = Spawns[math.random(1, #Spawns)]
if Player.Character and Player.Character:FindFirstChild('Humanoid') then
Player.Character.HumanoidRootPart.CFrame = RandomLocation.Position
end
end
end
Summary: Put the local RandomLocation = Spawns[math.random(1, #Spawns)] code in the for loop