Math.random doesn't work

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.

1 Like

How many spawns do you have?

spawnAreas[math.random(4, #spawnAreas)]

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)
2 Likes

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
2 Likes

I have 4 spawns right now, but I am planning to add more.
When I do

spawnAreas[math.random(1, #spawnAreas)]

It only teleports the players to the first spawn in the folder.

1 Like

I edited my post to include the part where the spawn updates. You need to update it per round.

2 Likes

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()?

Thanks.

1 Like

Look at my reply, that should fix the issue at hand.

2 Likes

Yes it is mandatory for it to update.

2 Likes

Don’t you have to put local GameAreaTeleport = spawnAreas[math.random(1, #spawnAreas)]?

1 Like

Try this script above, it should work.

2 Likes

No.
math.random automatically assigns the starting point as 1 if you don’t provide it with 2 arguments.

math.random(5) would pick a random number between 1 and 5.
math.random(1, 5) would do the exact same.

2 Likes

There is a new error with the script:
error2

1 Like

Which line? Also, my scripts does not reference a child.property named 2 unless that what a spawn is named.

2 Likes

At Line 18, which is

char.MoveTo(GameAreaTeleport[math.random(1, #spawnAreas)].Position)
1 Like

Tell me any errors/output with the line #.

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)
2 Likes

There isn’t anything printing.

1 Like

Did it work then?

1 Like

No, the script seems to be picking up the spawns randomly, but the players are not getting teleported.

1 Like

Try this and tell me the output.

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)
1 Like

I don’t know what 2 is representing in the error output, but it keeps on printing this error.