This is complicated... So I am making a script that randomly generates barriers throughout my tiny map. I want to make sure that there is a certain offset between the two, I know I would use magnitude, but how would I apply it in a script like this?
for i = 1, blocks do local block = game.ServerStorage.Disco:Clone() block.Name = "Barrier" block.Parent = partmodel block.Size = Vector3.new(1,math.random(7,10),math.random(15,21)) block.Anchored = true block.CFrame = CFrame.new(math.random(-150,-100),game.Workspace.Platform.Position.Y block.Size.Y/2 + game.Workspace.Platform.Size.Y/2,math.random(35,102)) for i = 1,block.Size.Y do block.CFrame = block.CFrame * CFrame.new(0,1,0) wait(0) end local safe = Instance.new("Part",block) safe.Name = "Safe" safe.Size = Vector3.new(100,block.Size.Y,block.Size.Z) safe.Anchored = true safe.CanCollide = false safe.Transparency = 1 safe.CFrame = CFrame.new(block.Position.X + safe.Size.X/2 + block.Size.X/2,block.Position.Y,block.Position.Z) local imp = script.Script:Clone() imp.Parent = safe wait(1) end end
Given a point p
, you can determine what the distance is to all of the barriers that have been placed so far. If all of those distances are bigger than the separation
you want, you can place a barrier there.
It might look something like this:
local barriers = {} -- start with none for i = 1, blocks do local p = Vector3.new( math.random()*250 - 150, workspace.Platform.Position.y, math.random() * 37 + 35 ) local nearest = math.huge for _, barrier in pairs(barriers) do nearest = math.min(nearest, (barrier - p).magnitude) end if nearest > separation then table.insert(barriers, p) BuildBarrier(p) -- where BuildBarrier makes a barrier at position p end end