so i have trees and i want to be able to spawn them in random locations but only within boundaries, i will be having multiple of these boundaries too
You’re going to need to use math.random()
and use x, y, and x coordinates to make the parameters
I searched it up and found this post: How could I make a part spawn randomly in a given location?
Basically , you just need to do something like this:
local Radius = 10
local x = math.random(0, Radius)
local y = math.random(0, Radius)
local z = math.random(0, Radius)
Part.Position = Vector3.new(x, y, z)
Actually I think this is much better.
local radius = 15 -- The length of the boundary / 2
local boundary1Center = Vector3.new(5,0,9) -- Where you want the center of the boundary to be
while true do
task.wait(10) -- Delay for each tree spawning
local x = math.random(boundary1Center.X - radius, boundary1Center.X + radius)
local y = boundary1Center.Y
local z = math.random(boundary1Center.Z - radius, boundary1Center.Z + radius)
local tree = Instance.new("Part", game.Workspace) -- Replace this with tree model
tree.Name = "Tree"
tree.Position = Vector3.new(x,y,z)
end
Edit: You could make multiple boundaryCenters and multiple radius’s, up to you!
I made a small typo so I fixed it
Yeah that is better I just got that code from the post I linked which should help
how do i set this up to place my models randomly?
sorry for late response, ive been modeling the axe and coding it to cut the tree down
Here’s an example, you wanted trees to spawn in a 40x40 area where the center of the boundary is 50, 5, 24.
You would do…
local radius = 20 -- The length of the boundary / 2 because the boundary is 40x40
local boundary1Center = Vector3.new(50,5,24) -- Where you want the center of the boundary to be
while true do
task.wait(10) -- Delay for each tree spawning
local x = math.random(boundary1Center.X - radius, boundary1Center.X + radius)
local y = boundary1Center.Y
local z = math.random(boundary1Center.Z - radius, boundary1Center.Z + radius)
local templateTree = game.ReplicatedStorage:FindFirstChild("TreeModel")
local tree = templateTree:Clone
tree.Parent = workspace
tree.Position = Vector3.new(x,y,z)
end
If you had multiple boundaries you would just…
- make a radius2 or radius3
- make a boundary2Center or boundary3Center
- either duplicate the while true loop or add it on there, sorry but I can’t do it for you
sorry i forgot to specify i want them to spawn on a part like this:
so i want to place a part, add a script or name it something, then it becomes a “spawn pad” for trees
also can you make sure trees dont spawn inside eachother?
I’m still kinda new to coding so I don’t know how to resolve your 2nd issue, but I can resolve the first.
Do this:
local treeZone = script.Parent -- Parent the script to the tree zone
local center = treeZone.Position -- Since the position is automatically the center, this works great!
local x = treeZone.Size.X
local xRadius = x / 2
local z = treeZone.Size.Z
local zRadius = z / 2
while true do
task.wait(10) -- Delay for each tree spawning
local x = math.random(center.X - xRadius, center.X + xRadius)
local y = center.Y
local z = math.random(center.Z - zRadius, center.Z + zRadius)
local templateTree = game.ReplicatedStorage:FindFirstChild("TreeModel")
local tree = templateTree:Clone
tree.Parent = treeZone -- was previously workspace
tree.Position = Vector3.new(x,y,z)
end
Please tell me if you found any bugs with my code!
Also, if you want multiple zones, read my reply for when you asked how to place your models randomly.
OOPS, I meant Pivot, I forgot models don’t have CFrames and Positions.
to get a trees position into a variable, use GetPivot(Tree)
to teleport the tree to another place, use PivotTo(Location)
Im still new to pivoting so if you’re confused you should read documentation im busy rn so i dont have time sry
ok that works, imma make some adjustements to ur code to fit my needs, i just needed the random placement
i made some changes, if you want this is what the script looks like now:
local treeZone = script.Parent -- Parent the script to the tree zone
local center = treeZone.Position -- Since the position is automatically the center, this works great!
local x = treeZone.Size.X
local xRadius = x / 2
local z = treeZone.Size.Z
local zRadius = z / 2
local Spawned = 0
local event = script.Parent.Removed
local function RTree()
return game.ReplicatedStorage.Trees:GetChildren()[math.random(1, #game.ReplicatedStorage.Trees:GetChildren())]:Clone()
end
event.Event:Connect(function()
Spawned -= 1
end)
while true do
task.wait(3) -- Delay for each tree spawning
if Spawned ~= script.Parent.Max.Value then
local x = math.random(center.X - xRadius, center.X + xRadius)
local y = center.Y
local z = math.random(center.Z - zRadius, center.Z + zRadius)
local tree = RTree()
tree.Parent = treeZone -- was previously workspace
tree:PivotTo(CFrame.new(x,y,z))
local TreeOffset = CFrame.Angles(0, math.rad(math.random(1, 360)), 0)
tree:SetPrimaryPartCFrame(tree:GetPrimaryPartCFrame() * TreeOffset)
Spawned += 1
end
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.