How would I make objects not spawn in each other?

So, I’m trying to make fruits spawn on trees. I’m trying to make them not spawn in of each other, not the same position. How would I do that? Btw pos is a part.

Code so far:

local fruits = workspace.Fruits
local vegetation = workspace.Vegetation

for _, veg in pairs(vegetation:GetChildren()) do
	local spawnPos = veg.SpawnPos
	local fruitToSpawn = veg.FruitToSpawn
	
	spawn(function()
		while wait(15) do
			local pos = spawnPos:GetChildren()[math.random(1, #spawnPos:GetChildren())]
			
			local fruit = fruitToSpawn.Value:Clone()
			fruit.Position = pos.Position
			fruit.Parent = fruits
		end
	end)
end

https://gyazo.com/f278d0545bc8c1379bfb20b4e571ea88

Oh, I ran into this Problem before…
What you wanna do is When they spawn make a Touch event

So like

Fruit.Touched:Connect(function(hit)
if hit.Name == "Fruit" then
Fruit:Destroy()
end)

I guess that would work, I’ll try that right now

It works very well, thank you so much! :grinning:

1 Like