How do i use math.random

im trying to randomly generate something with math.random and here is the script
local orb = game.Lighting.XP

while true do
	local clone = orb:Clone()
	clone.Parent = workspace.orbs
	clone.Position = math.random(-2097.074, 14.905, 798.157, 2883.039, 9.157, -4183.502)
	game.workspace.orbs.OrbsValue.Value = game.workspace.orbs.OrbsValue.Value + 1
	wait()
	if game.Workspace.orbs.OrbsValue.Value >= 100 then
		break
	end
end
3 Likes

This post was flagged by the community and is temporarily hidden.

2 Likes

but then what do i do because i thought you put in the coordinates

math.random() can only be used with 2 parameters, the min number and max number. If you want to get 6 possible numbers just do:

local Numbers  = {22, 30, 10, 4, 5}

local RandomNumber = math.random(1, #Numbers)

print(Numbers[RandomNumber])
2 Likes

so this
local orb = game.Lighting.XP

while true do
	local clone = orb:Clone()
	clone.Parent = workspace.orbs
	local map = "-2097.074, 14.905, 798.157, 2883.039, 9.157, -4183.502"
	clone.Position = math.random(#map)
	game.workspace.orbs.OrbsValue.Value = game.workspace.orbs.OrbsValue.Value + 1
	wait()
	if game.Workspace.orbs.OrbsValue.Value >= 100 then
		break
	end
end
local randomnumber = math.Random(1,2)
if randomnumber == 1 then
    clone.CFrame = CFrame.new(-2097.074, 14.905, 798.157)
else
    clone.CFrame = CFrame.new(2883.039, 9.157, -4183.502)
end
1 Like

yea but i want the xp orbs randomly around the whole map

1 Like

No, if you want to make a random position just do:

local Positions  = {
	Vector3.new(20, 20, 20);
	Vector3.new(10, 10, 10)
} -- Put here the possible positions

local RandomNumber = math.random(1, #Positions)

print(Positions[RandomNumber])

The math.random function has the following parameters and behaviors:

  • math.random(): Returns a random number between 0-1
  • math.random(x): Return a random integer between 0 and x
  • math.random(x, y): Returns a random integer between x and y

In your case, I’m going to make a guess that you have 2 different positions you want to be randomly chosen? In that case, you can throw those positions into a table, and then have math.random pick out a random index in the table, which will point to one of the positions.

local positions = {
   Vector3.new(-2097.074, 14.905, 798.157),
   Vector3.new(2883.039, 9.157, -4183.502),
}

...

local randomPosition = positions[math.random(1, #positions)]
clone.Position = randomPosition
4 Likes

so the vectors are the posible positions?

Yes, Vector3 refers to X, Y, Z

It would be a number value, cause integers can’t contain decimals.

oh ok thanks even though my name is denys_developer i am not that good at scripting

so, the script is making this

this will make the orb go to the position that as randomly choosed

Only the parameterless version returns a decimal number. The other ones return integers.

If you’re wanting to randomly assign orbs around the map, you’d define X and Z as variables, carrying a random number given from math.random(), and add it to the center. Here’s an example:

local int = 0
while wait() do
    int += 1
    local clone = orb:Clone(); clone.Parent = game.Workspace
    
    local x, z = math.random(-100, 100), math.random(-100, 100)
    clone.Position = Vector3.new(x, 5, z)
    
    if int == 100 then break end
end

also i forgot to menstion is that my map is not flat

Try this

while true do
	local clone = orb:Clone()
	clone.Parent = workspace.orbs
	local randomnumber = math.Random(1,2)
   if randomnumber == 1 then
       clone.CFrame = CFrame.new(-2097.074, 14.905, 798.157)
   elseif randomnumber == 2 then
       clone.CFrame = CFrame.new(2883.039, 9.157, -4183.502)
   end
	game.workspace.orbs.OrbsValue.Value = game.workspace.orbs.OrbsValue.Value + 1
	wait()
	if game.Workspace.orbs.OrbsValue.Value >= 100 then
		break
	end
end

my map is not flat so can it spawn on non flat serfices

Yea, I know, I used the location that u choosed in the other script