Okay there is clearly confusion, I do not want to delete all the children in the folder, as some are touching the baseplate. In fortnite, when someone shoots a build ( or base of the build ) down, then it will destroy the rest that is touching it, this is what I am attempting to do, I am not attempting to clear the folder.
Actually now that I think about it, whenever a player places anything I would also set an object value to the adjacent build. Like walls on a floor for instance. Then before the floor gets deleted I would queue the walls for deletion as well. This should create a sort of web or tree that causes any build that has no dependency to be deleted.
Alright. Whenever you find a part that is touching the base, just add it to a table and then return the table in the function and loop through the table deleting the parts. If you need help implementing that into your code, let me know.
I am back, I do need help implementing that, would I insert the part touching the baseplate to the table, and then the parts that are touching that part touching the baseplate in the table, and the parts touching the parts touching the baseplates? It’s a big pattern, but is this how it would be done? And then it checks if those parts exist, and if they don’t it all gets deleted?
Here is an implementation to your code:
local builds = game.Workspace.Builds
function recheck()
local list = {}
for i,v in pairs(builds:GetChildren()) do
if v then
for r,c in pairs(v:GetTouchingParts()) do
if c then
if c.Name == "Baseplate" then
table.insert(list,v)
else
for x,t in pairs(c:GetTouchingParts()) do
if t.Name == "Baseplate" then
table.insert(list,v)
end
end
end
end
end
end
end
return list
end
while wait(.1) do
local tr = recheck()
if #tr > 0 then
for _,v in pairs(tr) do
v:Destroy()
end
else
if tr == false then
print('false')
end
end
end
What I do is add all of the parts touching the baseplate to the table which is returned when you call recheck()
Hey! Thanks for the help, it’s deleting every build I place though?
All of the parts on the build are touching something that is touching the base plate then.
It’s keeping the builds that aren’t touching the baseplate?
https://i.gyazo.com/42f335d9982f6dce535c45b314faf36a.mp4
This is more what I was looking for;
https://i.gyazo.com/044ba948d9b038fb25a7ddbbf9985dce.mp4
This isn’t what I am receiving, and the part the builds are most likely touching is the player, and I want the build to stay if its touching the player, not all of the sudden delete, it is touching the baseplate.
This is what you described you wanted the script to do. Doing what Strucid did with when the base build is destroyed is a bit more difficult.
Wait no I meant the build destroys if it isn’t touching a part that is touching the baseplate, if the part is touching a part that is touching the baseplate it stays, if the part is also touching the baseplate it stays, I didn’t want it to destroy parts once placed on the baseplate? This would ruin the point of a building system
Just wondering, are the models anchored too?
Yes, they are actually mesh parts, they are anchored, and can collide is on.
Here’s a new script I made for you.
You’ll have to create an invisible hitbox for the part though which you add to the folder/group.
Here is the code:
local builds = game.Workspace.Builds
local function GetTouchingParts(part)
local connection = part.Touched:Connect(function() end)
local results = part:GetTouchingParts()
connection:Disconnect()
return results
end
local function check(folder)
local list = {}
for _,v in pairs(folder:GetChildren()) do
local base = false
local parts1 = GetTouchingParts(v)
for _,a in pairs(parts1) do
if a.Name == "Baseplate" then
base = true
else
local parts2 = GetTouchingParts(a)
for _,b in pairs(parts2) do
if b.Name == "Baseplate" then
base = true
end
end
end
end
if not base then
table.insert(list,v)
end
end
return list
end
while wait(.1) do
local result = check(builds)
if #result>0 then
for _,part in pairs(result) do
part:Destroy()
end
end
end
Here is the post from where I got the GetTouchingParts function from: (it only finds parts that are within the part, not touching the edge)
Does it have to be can collide on?
If you make it can collide off, you won’t be able to walk on the ramps though. I edited my message with a different solution.
So I make a part that surrounds that part?
yes and make it invisible and can collide off. sorry that this is such a messy way of doing this.
you are forgetting you must have a touch interest if the part is cancollide false, also your script does not check if the build is touching another build
note that while doing this with an invisible hitbox to make sure it checks for the other part’s hitbox instead of the actual other parts since the original part will be touching its own hitbox as well, falsely adding to the count
yes. this was implied in what I said.
also, it’s the other way around with the touch interest if the part is cancollidable.
I noticed a problem that could happen if someone had multiple parts on the ground and you destroyed the base of one of the builds.
This in theory should loop through all connected parts and return false if there is no valid base.
function baseplatecheck(part)
for r,c in pairs(part:GetTouchingParts()) do
if c then
if c.Name == "Baseplate" then
return true
elseif c.Name == "Hitbox" then
baseplatecheck(c)
else
return false
end
end
end
end
function recheck()
for i,v in pairs(builds:GetChildren()) do
if v then -- Make sure for this part you have a hitbox added.
baseplatecheck(v.Hitbox)
end
end
end