I'm making a game with random generation where it checks for collisions before anchoring the part. This is the code I'm using to do that:
local function createWall() -- This function generates a random wall on the map local w = Instance.new("Part") -- The wall object type local vars = {90, 180} -- The two orientations for the walls local var = vars[math.random(#vars)] -- Picks a random orientation out of the list w.Name = "Wall" -- Wall name w.Material = Enum.Material.Concrete -- Wall material w.BrickColor = BrickColor.new("Cashmere") -- Wall color w.Anchored = false w.CFrame = CFrame.new((math.round(math.random(-v,v))),10.5,(math.round(math.random(-v,v)))) -- Wall position w.Size = Vector3.new((math.round(math.random(12,40))),21,2) -- Wall size w.Orientation = Vector3.new(0,var,0) -- Rotates object to the randomly chosen direction w.Parent = f2 -- Puts the wall in the wall folder Walls = Walls + 1 -- Adds 1 to the wall variable wait(0) -- Short pause in between wall generation w.Orientation = Vector3.new(0,var,0) -- Re-rotates before anchoring, the object could turn while unanchored w.Anchored = print("Wall #" .. Walls .. " was generated.") -- Verifies generation in output end
I can't prevent that the walls are sometimes generated on the border of the map and go outside of the walls. I want to find out how to delete parts when they enter a certain area, see through them with glass walls, or move them to a random position with CFrame until they are in an area inside of the map borders.
Please, I've been trying and looking for 3 days.
Try using the SubtractAsync()
function to cut off parts. It's effectively the union negate function but as a function.
Just build a couple of invisible parts at the border which will be used to cut off the walls which fall outside.
Example:
local borders = {workspace.Border1,workspace.Border2,workspace.Border3,workspace.Border4} Wall:SubtractAsync(borders)
This will cut off the wall where it intersects with the border.