How to make random number of random fruit spawn on trees

Hi, so ive been trying to make this script for like an hour(headache) but couldn’t :confused:. What im trying to do is spawn a random number ranging from 1 to 7(7 spawns on every tree/bush) of random types of fruit spawn on every tree.

Current code on server script:

local CollectionService = game:GetService("CollectionService")

for i, v in pairs(workspace:GetDescendants()) do
	if CollectionService:HasTag(v, "FruitTree") then
		local Tree = v
		local Spawns = Tree:FindFirstChild("Spawns")
		
		if Spawns then
			if not Spawns:FindFirstChildOfClass("BasePart") then
				for i, spawnAmount in pairs(Spawns:GetChildren()) do
					local RanSpawnAmount = math.random(1, i)

					for count = 0, RanSpawnAmount, 1 do
						for i, berries in pairs(game.ServerStorage.Berries:GetDescendants()) do
							if berries:IsA("BasePart") then
								local clone = berries:Clone()
								clone.Position = spawnAmount.Position
								clone.Anchored = true
								clone.Parent = spawnAmount
							end
						end
					end
				end
			end
		end
	end
end

Screenshot 2024-01-30 175732

1 Like

For a random spawn amount, please do:

local spawns = spawnsfolder:GetChildren()
local randomspawn = math.random(1, #spawns
local spawn = spawns[randomspawn]

If you want a random amount of spawns simply do the same, just put it in a loop and use the randomspawn variable i provided as the iteration amount