Math.Random displaying wrong number of arguments

Hey, the error says [ServerScriptService.Gems_Spawn:9: wrong number of arguments]
Please help if you can!
Code:

local s1 = game.Workspace.Spawn
local s2 = game.Workspace.Spawn2
local s3 = game.Workspace.Spawn3
local s4 = game.Workspace.Spawn4
local gem = game.ServerStorage.Gems['Stage-1'].Gem
local r = 10
while wait(1) do
	local gemclone = gem:Clone()
	local spawner = math.random(s1,s2,s3,s4) --line 9
	local x,z = spawner.Position.X,spawner.Position.Z
	x,z = math.random(x - r,x + r),math.random(z - r,z + r)
	local pos = Vector3.new(x,spawner.Position.Y,z)
	gemclone.Parent = workspace.Gems
	gemclone.Position = pos
end

math.random can take at most 2 arguments:

math.random() -- returns [0, 1)
math.random(max) -- returns [0, max]
math.random(min, max) -- returns [min, max]

There are many ways to approach what you want to do:

local spawns = {
	game.Workspace.Spawn
	game.Workspace.Spawn2
	game.Workspace.Spawn3
	game.Workspace.Spawn4
}

local spawner = spawns[math.random(1, #spawns)]

I’d prefer just having all the spawns in some container, and having the table come from :GetChildren().

6 Likes

Try making your parts in a table and then using math.random with the table.

Example:

local Spawn = availableSpawns[math.random(#availableSpawns)]

dark beat me to it :frowning:

3 Likes

So;

local spawns = {
	game.Workspace.Spawn,
	game.Workspace.Spawn2,
	game.Workspace.Spawn3,
	game.Workspace.Spawn4,
}
local spawner = spawns[math.random(1, #spawns)]
local gem = game.ServerStorage.Gems['Stage-1'].Gem
local r = 10
while wait(1) do
	local gemclone = gem:Clone()
	local x,z = spawner.Position.X,spawner.Position.Z
	x,z = math.random(x - r,x + r),math.random(z - r,z + r)
	local pos = Vector3.new(x,spawner.Position.Y,z)
	gemclone.Parent = workspace.Gems
	gemclone.Position = pos
end

I am verifying I did it properly, as I am testing it right now

Using that will only make a randomised spawn once and not every one second which is what you were doing beforehand.

1 Like

Nope it worked, I tested it. It constantly spawned.

1 Like

You should move local spawner = spawns[math.random(1, #spawns)] inside the while-loop, otherwise you’ll only be choosing it once. In the original script, it seemed like choosing one of the 4 spawners every loop is what you wanted to do.