What do you want to achieve? Keep it simple and clear!
I want to use a flood fill algorithm to detect enclosed spaces and do actions accordingly.
What is the issue? Include screenshots / videos if possible!
My implementation won’t work. It just goes to the side and doesn’t stop when it detects a dead end.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Chatgpt was hopeless, didn’t do anything. I looked into some dev forum posts but they weren’t what I was looking for.
-- my implementation
local ship = script.Parent.Parent
local nodes = ship["Watertight Nodes"]
local attempts = 0
local function FloodFill(pos:Vector3)
task.wait()
attempts += 1
local brush = Instance.new("Part") do
brush.Size = Vector3.new(1, 1, 1)
brush.Position = pos
brush.Transparency = 1
brush.CanCollide = false
brush.Anchored = true
brush.Name = "brush"
brush.Parent = workspace
end
FloodFill(pos + Vector3.new(1, 0, 0))) -- right
FloodFill(pos + Vector3.new(-1, 0, 0)) -- left
FloodFill(pos + Vector3.new(0, 1, 0)) -- up
FloodFill(pos + Vector3.new(0, -1, 0)) -- down
FloodFill(pos + Vector3.new(0, 0, -1)) -- front
FloodFill(pos + Vector3.new(0, 0, 1)) -- back
end
wait(2)
FloodFill(nodes.Node1.Position)
I forgot to update, whoops!
Anyway this is the new code that is sorta working.
local ship = script.Parent.Parent
local nodes = ship["Watertight Nodes"]
local attempts = 0
local function FloodFill(pos:Vector3, prevPart:BasePart)
if attempts >= 100 then return; end
local shouldTest = true
if prevPart then
prevPart.Touched:Connect(function(otherPart)
print(otherPart, prevPart)
if otherPart.Name ~= "brush" then
shouldTest = false
end
end)
end
task.wait()
attempts += 1
if shouldTest then
local brush = Instance.new("Part") do
brush.Size = Vector3.new(1, 1, 1)
brush.Position = pos
brush.Transparency = 1
brush.CanCollide = false
brush.Anchored = true
brush.Name = "brush"
brush.Parent = workspace
end
FloodFill(pos + Vector3.new(0, 0, 1), brush) -- back
end
end
wait(2)
FloodFill(nodes.Node1.Position)
local attempts = 0
local function FloodFill(pos:Vector3, prevPart:BasePart)
if attempts >= 100 then return; end
local shouldTest = true
attempts += 1
if shouldTest then
local brush = Instance.new("Part") do
brush.Size = Vector3.new(1, 1, 1)
brush.Position = pos
brush.Transparency = 0
brush.CanCollide = false
brush.Anchored = true
brush.Name = "brush"
brush.Parent = workspace
end
local parts = workspace:GetPartsInPart(brush)
print(parts)
local shouldtestagain = true
for _, part in ipairs(parts) do
if part.Name ~= "brush" then
shouldtestagain = false
end
end
if shouldtestagain == true then
FloodFill(pos + Vector3.new(0, 0, 1), brush)
end
end
end
wait(2)
FloodFill(workspace.brush.Position)
try this
oh yeah dont forget to edit the variables
your method worked, just needed some changes.
here’s the final script.
local ship = script.Parent.Parent
local nodes = ship["Watertight Nodes"]
local attempts = 0
local olap = OverlapParams.new() do
olap.FilterType = Enum.RaycastFilterType.Exclude
olap.FilterDescendantsInstances = {nodes}
end
local attempts = 0
local function FloodFill(pos:Vector3)
if attempts >= 250 then return; end
task.wait()
local shouldTest = true
attempts += 1
if shouldTest then
local brush = game:GetService("ServerStorage").brush:Clone()
brush.Position = pos
brush.Parent = workspace
local parts = workspace:GetPartsInPart(brush, olap)
print(parts)
local shouldtestagain = true
--for _, part in ipairs(parts) do
-- if part.Name ~= "brush" then
-- shouldtestagain = false
-- end
--end
if #parts > 0 then
shouldtestagain = false
brush:Destroy()
end
if shouldtestagain == true then
FloodFill(pos + Vector3.new(0, 0, 1))
coroutine.wrap(FloodFill)(pos + Vector3.new(0, 0, -2))
coroutine.wrap(FloodFill)(pos + Vector3.new(-2, 0, 0))
coroutine.wrap(FloodFill)(pos + Vector3.new(2, 0, 0))
coroutine.wrap(FloodFill)(pos + Vector3.new(0, 2, 0))
coroutine.wrap(FloodFill)(pos + Vector3.new(0, -2, 0))
end
end
end
wait(5)
FloodFill(nodes.Node1.Position)