#How can i make a part spawn randomly in an area?

1 messages · Page 1 of 1 (latest)

clever egret
#

I need help because ive been trying to do this but cant. Can anyone help?

unborn yokeBOT
#

studio** You are now Level 2! **studio

pliant fulcrum
#

if you need a random position you can do smth like this

local random = Random.new()
local pos = Vector3.new(random:NextInteger(MIN X POS, MAX X POS), 10, random:NextInteger(MIN Z POS, MAX Z POS))

then just set it to the .Position of your part

unborn yokeBOT
#

studio** You are now Level 3! **studio

clever egret
#

does it work with models?

pliant fulcrum
#

probably but remember set the min/max positions (the caps text)

#

also might want to put that in a function to not have to find it every time

like

local random = Random.new()

local function GetRandomPos()
  return Vector3.new(random:NextInteger(MIN X POS, MAX X POS), 10, random:NextInteger(MIN Z POS, MAX Z POS))
end

and then smth like YourModel.Position = GetRandomPos()

rose moat
#

Hello Lina sorry for bothering you too TT

#

How can i add background on workspace?

#

like that

pliant fulcrum
rose moat
#

???

clever egret
#

btw lina does the x in the script mean times?

pliant fulcrum
rose moat
#

work

#

i have a one more question

#

where can i test assets?

#

from roblox

#

because i got banned yesterday for put one picture

#

this picture was

#

and i got banned

pliant fulcrum
pliant fulcrum
#

just replace the MIN X POS, MAX X POS, MIN Z POS, MAX Z POS in code with numbers
thatll make the rectangle where your model will be spawned

clever egret
#

ohhhh

#

where does the script go

rose moat
#

lina

#

where can i test assets?

pliant fulcrum
clever egret
#

thanks lina

rose moat
#

@pliant fulcrum

#

i got banned

#

BECAUSE YOU DON T TELL ME

#

WHERE CAN I TEST THE ASSETS

#

bullshit game

clever egret
#

@pliant fulcrum for some reason its says position is not a valid member of rock

pliant fulcrum
clever egret
#

Hi again now what should i do if i want multiple of the model spawning randomly

clever egret
#

ohhh ok

#

should i clone the model in the script or just duplicate it?

clever egret
#

@pliant fulcrum can i get some help?

pliant fulcrum
clever egret
#

should i put that in the same script or a different one?

pliant fulcrum
#

the same one but adapt it, not just copy paste

clever egret
#

yeah ik thanks ill ask for more if i get stumped

pliant fulcrum
#

kk

clever egret
#

should i make a repeat loop or a while loop?

clever egret
#

@pliant fulcrum ?

pliant fulcrum
#

with a while you’d have to have a variable and check it, but with for you can just for i = 1, 100 do

clever egret
#

should i put the for above the clone loop or the getrandompos function

#

@pliant fulcrum

clever egret
#

hello @pliant fulcrum ?

pliant fulcrum
clever egret
#

yep

#

This is it

local Spawns = game.Workspace.Spawns
local random = Random.new()
local model = Spawns.Rock

local function GetRandomPos()
    return Vector3.new(random:NextInteger(-62, -176.5), 4, random:NextInteger(-43.5, 61.5))
end

for i = 1, 20 do
    local clone = model:Clone()
    clone:MoveTo(GetRandomPos())
    clone.Parent = Spawns
end
pliant fulcrum
#

yea looks nice

clever egret
#

do i do the loop in the for i = 1 blah blah or what?

pliant fulcrum
#

wdym

clever egret
#

like the for loop is that for i = 1, 20 do
local clone = model:Clone()
clone:MoveTo(GetRandomPos())
clone.Parent = Spawns
end

pliant fulcrum
#

oh yea

unborn yokeBOT
#

studio** You are now Level 4! **studio

clever egret
#

how do i loop it?

pliant fulcrum
#

?

#

if you were to use while loop itd look like this

local i = 1
while i <= 20 do
  local clone = model:Clone()
  clone:MoveTo(GetRandomPos())
  clone.Parent = Spawns
  i += 1
end

works the same but for is imo more readable here

clever egret
#

oh no problem i fixed it. it were one line

#

is there a way to rotate the models too?

pliant fulcrum
#

uhh

#

honestly idrk

#

try replacing the MoveTo call with smth like

local angleX = random:NextValue() * 2 * math.pi
local angleY = random:NextValue() * 2 * math.pi
local angleZ = random:NextValue() * 2 * math.pi
local cframe = CFrame.new(GetRandomPos()) * CFrame.fromEulerAngles(angleX, angleY, angleZ)

clone:PivotTo(cframe)

like first you get the angles in radians (so from 0 to 2pi) and then make a CFrame out of the position and rotation angles

clever egret
#

like this?

local random = Random.new()
local model = Spawns.Rock

local function GetRandomPos()
    return Vector3.new(random:NextInteger(-62, -176.5), 4, random:NextInteger(-43.5, 61.5))
end

local function GetRandomRot()
    return 
end

for i = 1, 20 do
    local clone = model:Clone()
    clone.Parent = Spawns
    local angleX = random:NextValue() * 2 * math.pi
    local angleY = random:NextValue() * 2 * math.pi
    local angleZ = random:NextValue() * 2 * math.pi
    local cframe = CFrame.new(GetRandomPos()) * CFrame.fromEulerAngles(angleX, angleY, angleZ)
    clone:PivotTo(cframe)
end

pliant fulcrum
#

yea

clever egret
#

k ill try

#

it says NextValue is not a valid member of Random

unborn yokeBOT
#

studio** You are now Level 5! **studio

pliant fulcrum
#

oh its NextNumber not NextValue

#

my bad

clever egret
#

ok ill change it quick and retry

#

it works but i wanted to rotate around the y axis

pliant fulcrum
#

oh only around the y axis

clever egret
#

yep

pliant fulcrum
#

then remove angleX angleZ

clever egret
#

ok

pliant fulcrum
#

and in the fromEulerAngles make it (0, angleY, 0)

clever egret
#

thanks

#

ill give creds when game is finished

pliant fulcrum
#

no need

clever egret
#

oh and btw i have collision boxes on the models so they dont go inside eachother is there a way to do that

#

just asking

pliant fulcrum
#

idk

#

maybe you can add OnTouched to the models and check if its touching another model

#

if yes, change the position like how its done in the loop

#

tho idk how thatll be for performance

clever egret
#

yeah

#

would i make on touched a function

#

Just modify this script and ill test it

local random = Random.new()
local model = Spawns.Rock

local function GetRandomPos()
    return Vector3.new(random:NextInteger(-62, -176.5), 4, random:NextInteger(-43.5, 61.5))
end

local function GetRandomRot()
    return 
end

for i = 1, 20 do
    local clone = model:Clone()
    clone.Parent = Spawns
    local angleX = random:NextValue() * 2 * math.pi
    local angleY = random:NextValue() * 2 * math.pi
    local angleZ = random:NextValue() * 2 * math.pi
    local cframe = CFrame.new(GetRandomPos()) * CFrame.fromEulerAngles(angleX, angleY, angleZ)
    clone:PivotTo(cframe)
end

pliant fulcrum
#

dont rly have the time rn sry

#

just google robloxx ontouched

clever egret
#

ok

pliant fulcrum
#

should give a few tutorials

clever egret
#

thanks

clever egret
#

@pliant fulcrum you got time