Hey! So I am attempting to get a script to see if a part or build is touching a baseplate or touching another part that is touching the baseplate, and if it isn’t touching a part that is touching the baseplate, it would destroy it, how would I do this? Here is what I’ve been trying so far, it’s been printing how it should I think, but I am unsure how to continue;
local builds = game.Workspace.Builds
function recheck()
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
return true
else
for x,t in pairs(c:GetTouchingParts()) do
if t.Name == "Baseplate" then
return true
else
return false
end
end
end
end
end
end
end
end
while wait(.1) do
local tr = recheck()
if tr == true then
print('true')
else
if tr == false then
print('false')
end
end
end
The folder is a folder with all the builds in the map, that a person or other person(s) have placed, I am checking if the build was placed on another build that is touching the baseplate, if that build isn’t touching the baseplate, then is another build touching the build that is touching the build? If it isn’t, then it destroys all the builds it is touching, like a fortnite type of thing.
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?
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()
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.
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
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)