Math.random help

Sorry for posting a lot here, you can just ignore this if you don’t want to help. I just recently unlocked DevForum.

In my game, you can buy a car and it spawns on a pad. This pad FLOODS with cars, and it basically breaks the entire game. I’m trying to make it so the cars spawn on 1 of 6 pads, instead of 1. I’m trying to fiddle around with math.random, but I’m having trouble.

local function cloneFunc(vehicle)

vehicle.Parent = workspace

vehicle:MoveTo(Vector3.new(workspace.SpawnPad.Position.X, workspace.SpawnPad.Position.Y + 5, workspace.SpawnPad.Position.Z))

end

what, where is the math.random?

im trying to implement it into the script

i basically just showed the original script

what is then the problem, just do math.random(min,max) or use

local r=Random.new()
local result=r:NextInteger(min,max)

how do i implement that into the script

make folder named spawns, put all spawns there and name them 1-6 and do
local r=Random.new()
local spawn = workspace.spawns[r:NextInteger(1,6)]

This solution simply picks a random child of the spawns Model, so you can just put any spawns in there (and you don’t have to name them anything).

Using math.random(min, max)

local spawns = workspace.Spawns --Or whatever location you store the spawns at
local function cloneFunc(vehicle)
    vehicle.Parent = workspace
    local spawnPad = spawns:GetChildren(math.random(1, #spawns:GetChildren()))
    vehicle:MoveTo(Vector3.new(SpawnPad.Position + Vector3.new(0,5,0)) --Also simplified this line
end

Or using the Random.new function with :NextInteger(min, max) (works exactly the same way)

local randO = Random.new()
local spawns = workspace.Spawns --Or whatever location you store the spawns at
local function cloneFunc(vehicle)
    vehicle.Parent = workspace
    local spawnPad = spawns:GetChildren(randO:NextInteger(1, #spawns:GetChildren()))
    vehicle:MoveTo(Vector3.new(SpawnPad.Position + Vector3.new(0,5,0)) --Also simplified this line
end
1 Like

Cool tip: math.random() can take one argument, if it does then it’ll produce a random number between 1 and that argument.