I got it working with all the parts in the build folder, the only issue is that it is very laggy in studio when i have a ton of builds placed. robloxapp-20231124-1312060.wmv (2.2 MB)
this is the server script I used
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local MainPartsFolder = Workspace:WaitForChild("Builds")
local function RecursivelyCheckPart(Part, ExclusionList)
local TouchingParts = Part:GetTouchingParts()
for i = #TouchingParts, 1, -1 do
local touchingPart = TouchingParts[i]
if table.find(ExclusionList, touchingPart) then
table.remove(TouchingParts, i)
end
end
if #TouchingParts == 0 then
return false
end
for _, part in ipairs(TouchingParts) do
if part.Name == "Ground" then
return true
else
table.insert(ExclusionList, part)
local Touching = RecursivelyCheckPart(part, ExclusionList)
if Touching then
return Touching
end
end
end
return false
end
local function Check()
local MainParts = MainPartsFolder:GetChildren()
for _, MainPart in ipairs(MainParts) do
local Supported = false
Supported = RecursivelyCheckPart(MainPart, {MainPart})
if Supported then
MainPart.BrickColor = BrickColor.new("Bright green")
else
MainPart:Destroy()
end
end
end
while true do
task.wait(0.25)
pcall(Check)
end
Firstly, you should only be running the search and clean algorithm when a build is destroyed, not constantly.
The only reason I ran it in a while loop was to demonstrate how the algorithm itself works. I only intended for you to integrate the search into your existing build module.
It is extremely inefficient to run a check when nothing is changed. It’s like checking your fridge when there’s no food hoping there will be food there each time. It only makes sense to check your fridge when you know you just went shopping. lmao.
Do that before reading further.
if it still lags:
ill try optimizing the code a bit. The algorithm I went with is a DFS. This means the algorithm could go do a very deep search only to find no solution, which would lead to lag.
Before we do anything, its important to note that its easier to just set things instead of moving things out of a table. Lets try that first.
here’s the revised code:
local function RecursivelyCheckPart(Part, ExclusionList)
local TouchingParts = Part:GetTouchingParts()
local NewTouchingParts = {}
for _, touchingPart in ipairs(TouchingParts) do
if not ExclusionList[touchingPart] then
table.insert(NewTouchingParts, touchingPart)
end
end
if #NewTouchingParts == 0 then
return false
end
for _, part in ipairs(NewTouchingParts) do
if part.Name == "Ground" then
return true
else
ExclusionList[part] = true
local Touching = RecursivelyCheckPart(part, ExclusionList)
if Touching then
return Touching
end
end
end
return false
end
Thank you for your help, i think i understand it better now. Got the system working and now only runs the check when parts are destroyed, appreciate it!
edit - the game only lags when a build is destroyed and there is a lot of buildings in the game, i might have a fix for it but just wanted to say this on here