Randomize Tree Spawns

Hi, I have a tree script. This script makes the tree fall after 5 hits with an axe. The tree respawns in the same place. I want to know how to make the tree spawns be more randomized. The location would be randomized inside of an invisible, non collidable, part.

8 Likes

okay so this is what you need to do.

you get the position by doing this:
local radius = 10 — set the radius of the random position
local randomTreePosition = vector3.new(math.random(-radius, radius), 0, math.random(-radius, radius))

then you raycast on that position to get the height (Y)

and you set the trees position doing:
randomTreePosition + vector3.new(0, raycast.Hit.Position.Y, 0)

3 Likes

Would the radius just be the size of the invisible part? I already have a part that is supposed to be the range

2 Likes

technically you dont need the part. If you really want to keep it for visualisation you can do this.

local part = workspace.part
local xRadius = part.Size.X/2
local zRadius = part.Size.Z/2
local randomTreePosition = part.Position + vector3.new(math.random(-xRadius , xRadius ), 0, math.random(-zRadius , zRadius ))
5 Likes

I’m going to interpret this as you have a bunch of predetermined positions that you made in a folder/group.

First, do something like this:
I will assume your spawn points are in Workspace, named “TreeSpawns”

local TreeSpawnsModel = workspace.TreeSpawns
local TreeSpawns = TreeSpawns:GetChildren()
local TotalSpawns = rawlen(TreeSpawns)
local Rand = Random.new()

local function GetRandomSpawn()
  local RandomPos = Rand:NextInteger(1, TotalSpawns)
  local Spawn = TreeSpawns[RandomPos]
  -- Spawn is now your spawn part, and you can clone and position your tree to it. Don't forget to parent the new tree.
  return Spawn -- Return the spawn part so we can use this as a function.
end

-- Assume this is used in a spawn loop (every 60 seconds or so)
while true do
  task.wait(60)
  local NewSpawn = GetRandomSpawn()
  -- You can do your spawn logic with your trees with the NewSpawn variable.
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.