How to make random spawn with math.random

how to make random spawn with math.random also my baseplate is not flat and its terrain thats mostly why i dont know how to make this and here is the terrain


here is the script:

local orb = game.Lighting.XP

while true do
    	local clone = orb:Clone()
    	clone.Parent = workspace.orbs
    	game.workspace.orbs.OrbsValue.Value = game.workspace.orbs.OrbsValue.Value + 1
    	wait()
    	if game.Workspace.orbs.OrbsValue.Value >= 100 then
    		break
    	end
    	clone.Position = math.random() -- here i dont know how to randomize this
    end
3 Likes

you could do clone.Position = Vector3.new(math.random(), math.random(), math.random())

You can do

Vector3.new(math.random(), math.random(), math.random())

yea but i dont have flat terrain

i dont think Vector3.new(math.random(), math.random(), math.random()) can put the xp orbs on a non flat terrain but i havent tried

Though it’s a little more tedious, you could create a randomized spawning system using spawns for said orbs. Of course, this method has benefits (stop the orbs from spawning in certain locations (such as walls)). The randomizer script is something along the lines of:

local orbspawn = game.Workspace.Spawns:GetChildren()[math.random(1, #game.Workspace.Spawns:GetChildren())]

@Fid6et what do i have to do to make this line of code work because im confused what it does

That line of code randomly selects an instance in another instance.

local orbspawn =--[[defines the variable]] game.Workspace.Spawns:GetChildren()--[[gets the children]][math.random(1, #game.Workspace.Spawns:GetChildren()--[[gets the number of children and uses math.random to select a random child by using index]])]

It’s kind of like using local part = game.Workspace.Part, as it gets an instance and defines it in a variable.

I would personally actually use perlin noise instead so you get a even distribution

1 Like

I’d recommend getting a random X and Y position by using math.random() between predetermined ranges that you want the XP Orbs to spawn in. Once you have those you can create a Vector3 with your randomized X and Y coordinates, and a Y coordinate of about 10 studs above the highest point in your map.

Use workspace:Raycast() to create a raycast from the randomized Vector3 pointing towards the ground and spawn the orb a few studs above where they meet.

Hello, is this more of what you’re asking for? If you have any questions feel free to ask.

--|< Varaibles >|--
local orb = game.Lighting:WaitForChild("XP");
local orbsFolder = workspace:WaitForChild("orbs");
local orbsValue = orbsFolder:WaitForChild("OrbsValue");

local maxOrbs = 100;
local waitTime = 0.1;

--|< Main  >|--
while orbsValue.Value < maxOrbs do
    local clone = orb:Clone();
    clone.Parent = orbsFolder;

    orbsValue.Value = orbsValue.Value + 1;

    local randomX = math.random(-50, 50);
    local randomY = math.random(5, 20);
    local randomZ = math.random(-50, 50);
    clone.Position = Vector3.new(randomX, randomY, randomZ);

    task.wait(waitTime);
end