#How can i make a part spawn randomly in an area?
1 messages · Page 1 of 1 (latest)
** You are now Level 2! **
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
** You are now Level 3! **
does it work with models?
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()
Hello Lina sorry for bothering you too TT
How can i add background on workspace?
like that
(a) thats a skybox, (b) make a separate thread instead of clogging this one :/
???
btw lina does the x in the script mean times?
just google roblox studio skybox
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
and also where does this go?
no, all objects in the world have positions in 3 axes like math vectors
x,y,z are the values along each axis
Vector3 is a container of sorts, thats used to store the positions, or basically anything that consists of 3 numbers
oh ok
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
in serverscripts would work
https://discord.com/channels/448986884497211392/449312743116505101 has some info on basic scripting if you need help
thanks lina
@pliant fulcrum
i got banned
BECAUSE YOU DON T TELL ME
WHERE CAN I TEST THE ASSETS
bullshit game
@pliant fulcrum for some reason its says position is not a valid member of rock
oh sry for models it should be not YOUR MODEL.Position = YOUR POS but YOUR MODEL:MoveTo(YOUR POS)
Hi again now what should i do if i want multiple of the model spawning randomly
put it in a loop
@pliant fulcrum can i get some help?
if you want multiple models just clone the original
like
local model = wherever your model is
for i = 1, how many clones do
local clone = model:Clone()
clone:MoveTo(some random position)
clone.Parent = workspace
end
should i put that in the same script or a different one?
the same one but adapt it, not just copy paste
yeah ik thanks ill ask for more if i get stumped
kk
should i make a repeat loop or a while loop?
@pliant fulcrum ?
i’d say best would be a for loop, it’s the easiest to understand here
with a while you’d have to have a variable and check it, but with for you can just for i = 1, 100 do
should i put the for above the clone loop or the getrandompos function
@pliant fulcrum
hello @pliant fulcrum ?
you should have smth like
local function GetRandomPos()
blah blah blah
end
for blah blah blah do
blah blah blah
end
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
yea looks nice
do i do the loop in the for i = 1 blah blah or what?
wdym
like the for loop is that for i = 1, 20 do
local clone = model:Clone()
clone:MoveTo(GetRandomPos())
clone.Parent = Spawns
end
oh yea
** You are now Level 4! **
how do i loop it?
?
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
oh no problem i fixed it. it were one line
is there a way to rotate the models too?
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
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
yea
** You are now Level 5! **
oh only around the y axis
yep
then remove angleX angleZ
ok
and in the fromEulerAngles make it (0, angleY, 0)
no need
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
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
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
ok
should give a few tutorials
thanks
@pliant fulcrum you got time