So, as the title is saying, im trying to create a room detection system, for my grid build mode. I have searched everywhere, tried to ask help from the community it self, and this is the only thing I have camen up with:
local Module = {}
local Rep = game:GetService('ReplicatedStorage')
local Node = Rep.Node
function IsThisRoom(wall, fhit)
local itIs = false
local tries = 0
if not itIs then
tries = tries + 1
local tp = wall:GetTouchingParts()
if #tp < 1 then
return false
else
for _, hit in pairs(tp) do
if hit.Name == "1" or hit.Name == '2' then
if hit ~= fhit then
if not itIs then
if hit.Parent == fhit.Parent then
itIs = true
ThisIsRoom(wall.Parent)
else
rec(hit, wall)
end
end
end
end
end
end
else
return
end
return itIs
end
function ThisIsRoom(room)
local color = BrickColor.Green()
for _, wall in pairs(room:GetChildren()) do
wall.BrickColor = color
end
end
local WALLS = {}
function Module:MakeRoom()
local walls = workspace.Assets.Walls
walls.ChildAdded:Connect(function(Child)
local Model = workspace:FindFirstChild('Wall')
if Model == nil then
Model = Instance.new("Model", workspace)
Model.Name = "Wall"
end
wait(0.05)
Child.Parent = Model
table.insert(WALLS, Child)
end)
for v, wall in pairs(WALLS) do
for k, wall2 in pairs(WALLS) do
for a, i in pairs(wall2.Hitbox:GetChildren()) do
for b, n in pairs(wall.Hitbox:GetChildren()) do
rec(i, n)
end
end
--rec(wall2.Hitbox, wall.Hitbox)
end
end
end
function rec(wall, preferedwall)
if preferedwall == nil then
local localParent = wall.Parent
--Touch
local t = wall.Touched:Connect(function() end)
local tp = wall:GetTouchingParts()
t:Disconnect()
if #tp < 1 then return end
for _, hit in ipairs(tp) do
if hit.Name == "1" or hit.Name == '2' then
local otherParent = hit.Parent
if otherParent ~= localParent then
wall.Parent = otherParent
local room = IsThisRoom(wall, hit)
if room then
ThisIsRoom(otherParent)
wall.Parent.Name = "ROOM"
end
else
end
end
end
else
wall.Parent = preferedwall.Parent
local room = IsThisRoom(wall, preferedwall)
if room then
ThisIsRoom(preferedwall.Parent)
wall.Parent.Name = "ROOM"
end
end
end
workspace.Assets.Walls.ChildAdded:Connect(function(Child)
local Model = workspace.Assets.Walls
wait(0.05)
Child.Parent = Model
table.insert(WALLS, Child)
for v, wall in pairs(WALLS) do
if wall:FindFirstChild('Hitbox') then
for b, n in pairs(wall.Hitbox:GetChildren()) do
rec(n)
end
end
end
end)
return Module
Gif:
https://i.gyazo.com/64a7a5011acd59cf274eb5bd065dd6d9.gif
When a node is green, that means its a room.
I really hope that someone could help me out, thanks