Randomized "rock" spawning

Hello!
What do you want to achieve?, Basically what I am trying to do is to make randomized rock spawning on my map.

What is the issue? The issue is that I have some decorations on my map and I don’t want any rocks in those areas. ( the red areas mean not to generate rocks in it)

–after I run the game

So how would I generate rocks outside of those red areas?

local Rocks = {
	["StarterRocks"] = {
		["FirstRocks"] = {
			--	["Position"] = {-479.141,-304.237,2.29,-156.5,143.3},
			["Position"] = {90,305,-30,-120,172},
			["Size"] = {7,10,5,10,3,8},
			["Orientation"] = {0,360}
		},
		["FirstRocks2"] = {
			--	["Position"] = {-479.141,-304.237,2.29,-156.5,143.3},
			["Position"] = {90,305,-30,-120,172},
			["Size"] = {9,13,8,13,6,9},
			["Orientation"] = {0,360}
		},
		["FirstRocks3"] = {
			--	["Position"] = {-479.141,-304.237,2.29,-156.5,143.3},
			["Position"] = {90,305,-30,-120,172},
			["Size"] = {14,17,10,15,9,12},
			["Orientation"] = {0,360}
		}
	}
}


--left upper -304.019, 2.289, -156.861
--right buttom -479.141, 2.289, 143.5.

local serverStorage = game:GetService("ServerStorage")
local starterBarrier = {}
for i,v in pairs(workspace.NoRocksHere.starter_map_barriers:GetChildren()) do
	table.insert(starterBarrier,v)
end
for i=0,15,1 do
	for _,y in pairs(serverStorage:WaitForChild("Rocks"):GetChildren()) do
		for i,v in pairs(y:GetChildren()) do
			local v = v:Clone()
			local rockhitbox = v.RockHitbox
			local Table = Rocks[y.Name][v.Name]
		
			local Postion = Vector3.new(math.random(Table.Position[1],Table.Position[2]), Table.Position[3],math.random(Table.Position[4],Table.Position[5]))
			local Size = Vector3.new(math.random(Table.Size[1],Table.Size[2]), math.random(Table.Size[3],Table.Size[4]), math.random(Table.Size[5],Table.Size[6]))
			local Orientation = Vector3.new(0, math.random(Table.Position[1],Table.Orientation[2]), 0)
			v.Parent = workspace:WaitForChild("Rocks")
			v.Position = Postion
			v.Size = Size
			v.Orientation = Orientation
			rockhitbox.Position = Postion
			rockhitbox.Size = Size + Vector3.new(2,2,2)
			rockhitbox.Orientation = Orientation
		end
	end
end
2 Likes

You can use workspace:GetPartsInPart(…) to check if the rock is in the area and move to the other place instead.

2 Likes

I thought of that as well but that would be bad for performance I think, even after I move the rock it might still be within another barrier and I would have to do the same thing over n over again (correct me if I am wrong)

1 Like

but that would be bad for performance I think,

Not really. All you are doing is moving a part a couple of times. Only time you may have performance issues is if you are constantly moving hundreds of parts at once, which you aren’t!

2 Likes

You could just store every part you dont want the rocks to spawn in position and then add a radius that determines if the rock is spawning on the wanted location, once that is done you could just check the rocks position and if it is near the radius skip the location.

local distance = (child --Parts you dont want the rocks to spawn in-- .Position - rock.Position).Magnitude

if distance < child.Size.Magnitude + rock.Size.Magnitude then
rock:Destroy() --or simply avoid the script generating it–
else
rock generating here
end

1 Like

right but it is not just moving the parts, I am running :GetPartsInPart multiple times in a short time interval which could possibly be bad right?

I have decided to use :GetPartsInPart and OverlapParams. I used a whitelist to only detect barriers and just check if the array was longer than 0. (this is overall better 4 performance cuz I won’t have to use For loop and it will also detect barriers only, thanks to everyone!)