Hello, I am trying to make a system that will randomly choose a part, and teleport players. When I do
use math.random, the game doesn’t choose the part randomly, instead they just pick one part, and keep on teleporting the players to the chosen part.
The spawnsFolder is located in Workspace.
This is a script inside ServerScriptService.
Here is the the script (I followed TheDevKing’s YouTube tutorial on how to make intermission, and GamerM8’s tutorial on how to make randomly picked maps.):
local spawnsFolder = game.Workspace.Spawns
local spawnAreas = game.Workspace.Spawns:GetChildren()
local roundLength = 100
local intermissionTimer = 25
local inRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status
local Lobby = game.Workspace.LobbyTeleport
local GameAreaTeleport = spawnAreas[math.random(4, #spawnAreas)]
inRound.Changed:Connect(function()
wait(1)
if inRound.Value == true then
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char:MoveTo(GameAreaTeleport.Position)
end
else
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char.HumanoidRootPart.CFrame = Lobby.CFrame
end
end
end)
local function roundTimer()
while wait() do
for i = intermissionTimer, 1, -1 do
inRound.Value = false
wait(1)
Status.Value = "Intermission: ".. i .." seconds left!"
end
for i = roundLength, 1, -1 do
inRound.Value = true
wait(1)
Status.Value = "Game: ".. i .." seconds left!"
end
end
end
spawn(roundTimer)
Is there a solution to choose the spawns randomly? Thank you.
This is only choosing from #4 spawn to the last spawn. Change it to
spawnAreas[math.random(1, #spawnAreas)]
to pick from all spawns.
New Script:
local spawnsFolder = game.Workspace.Spawns
local spawnAreas = game.Workspace.Spawns:GetChildren()
local roundLength = 100
local intermissionTimer = 25
local inRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status
local Lobby = game.Workspace.LobbyTeleport
local GameAreaTeleport = spawnAreas[math.random(1, #spawnAreas)]
inRound.Changed:Connect(function()
wait(1)
GameAreaTeleport = spawnAreas[math.random(1, #spawnAreas)]
if inRound.Value == true then
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char:MoveTo(GameAreaTeleport.Position)
end
else
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char.HumanoidRootPart.CFrame = Lobby.CFrame
end
end
end)
local function roundTimer()
while wait() do
for i = intermissionTimer, 1, -1 do
inRound.Value = false
wait(1)
Status.Value = "Intermission: ".. i .." seconds left!"
end
for i = roundLength, 1, -1 do
inRound.Value = true
wait(1)
Status.Value = "Game: ".. i .." seconds left!"
end
end
end
spawn(roundTimer)
You always use the same “randomly” picked spawn because you set it as a variable and always use that same exact variable.
You should pick a random spawn right before teleporting the player to it:
for _, player in pairs(game.Players:GetChildren()) do
local GameAreaTeleport = spawnAreas[math.random(#spawnAreas)]
local char = player.Character
char:MoveTo(GameAreaTeleport.Position)
end
I already put GameAreaTeleport = spawnAreas[math.random(1, #spawnAreas)]
at the top, but is it mandatory to put that again in inRound.Changed:Connect(function()?
local spawnsFolder = game.Workspace.Spawns
local spawnAreas = game.Workspace.Spawns:GetChildren()
local roundLength = 100
local intermissionTimer = 25
local inRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status
local Lobby = game.Workspace.LobbyTeleport
local GameAreaTeleport = spawnAreas[math.random(1, #spawnAreas)]
inRound.Changed:Connect(function()
wait(1)
GameAreaTeleport = spawnAreas[math.random(1, #spawnAreas)]
if not GameAreaTeleport then print("GameAreaTeleport does not exist") end
if inRound.Value == true then
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char:MoveTo(GameAreaTeleport.Position)
end
else
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char.HumanoidRootPart.CFrame = Lobby.CFrame
end
end
end)
local function roundTimer()
while wait() do
for i = intermissionTimer, 1, -1 do
inRound.Value = false
wait(1)
Status.Value = "Intermission: ".. i .." seconds left!"
end
for i = roundLength, 1, -1 do
inRound.Value = true
wait(1)
Status.Value = "Game: ".. i .." seconds left!"
end
end
end
spawn(roundTimer)
local spawnsFolder = game.Workspace.Spawns
local spawnAreas = game.Workspace.Spawns:GetChildren()
local roundLength = 100
local intermissionTimer = 25
local inRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status
local Lobby = game.Workspace.LobbyTeleport
local GameAreaTeleport = spawnAreas[math.random(1, #spawnAreas)]
inRound.Changed:Connect(function()
wait(1)
GameAreaTeleport = spawnAreas[math.random(1, #spawnAreas)]
if not GameAreaTeleport then print("GameAreaTeleport does not exist") end
if inRound.Value == true then
print(typeof(GameAreaTeleport))
print(GameAreaTeleport.Position)
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char:MoveTo(GameAreaTeleport.Position)
end
else
for _, player in pairs(game.Players:GetChildren()) do
local char = player.Character
char.HumanoidRootPart.CFrame = Lobby.CFrame
end
end
end)
local function roundTimer()
while wait() do
for i = intermissionTimer, 1, -1 do
inRound.Value = false
wait(1)
Status.Value = "Intermission: ".. i .." seconds left!"
end
for i = roundLength, 1, -1 do
inRound.Value = true
wait(1)
Status.Value = "Game: ".. i .." seconds left!"
end
end
end
spawn(roundTimer)