# Help with the grass generator

Hi! I wrote a code that should spawn the grass around the player (in this case, part), everything works fine, but I need to make sure that the grass does not spawn in places where it already exists (as shown in the video). Help please. Thank you in advance! Code:

``````local Limit = 50
local Current = 0
local Spawner = workspace.GrassSpawner
local Distance = 50
local LastPosition = 0
local function spawnOfEars()
for i = Current, Limit - 1 do
Current += 1
local RandomPosition = Vector3.new(math.random(Spawner.Position.X - Distance, Spawner.Position.X + Distance), Spawner.Position.Y, math.random(Spawner.Position.Z - Distance, Spawner.Position.Z + Distance))
local ray = Ray.new(Vector3.new(RandomPosition.X, Spawner.Position.Y + 50, RandomPosition.Z), Vector3.new(RandomPosition.X, Spawner.Position.Y - 100 * 10000376832, RandomPosition.Z))
local TallGrass = game:GetService("ReplicatedStorage").TallGrassFolder.TallGrass:Clone()
local Hit, hitPosition = workspace:FindPartOnRay(ray, character)
if Hit and Hit.Parent == workspace.Map.GrassFolder then
TallGrass.Parent = workspace.Map.TallGrassFolder
TallGrass.Position = Vector3.new(math.round(hitPosition.X), Hit.Position.Y + (Hit.Size.Y / 2 + TallGrass.Size.Y / 2), math.round(hitPosition.Z))
else
Current -= 1
end
end
for i, v in pairs(workspace.Map.TallGrassFolder:GetChildren()) do
if (Vector3.new(Spawner.Position.X, 0, 0) - Vector3.new(v.Position.X, 0, 0)).Magnitude > Distance or (Vector3.new(0, 0, Spawner.Position.Z) - Vector3.new(0, 0, v.Position.Z)).Magnitude > Distance then
v:Destroy()
Current -= 1
end
end
end
spawnOfEars()
Spawner.Changed:Connect(function()
if Spawner.Position ~= LastPosition then
spawnOfEars()
end
end)
``````
5 Likes