Log in to vote
1

# More reliable method than onTouched?

Asked by 4 days ago
Edited 4 days ago

Made a big brick that destroys certain parts it touches - I.E. a part with a specific name. It works wonders when destroying a players limbs and torso, but doesn't consistently work when destroying other parts; it may destroy a part near the player, or one the player is standing on, but it's inconsistent and never destroys the same parts upon contact when testing.

This is the script I have so far, are there any adjustments or alternative methods I could use to make the destruction more reliable?

-- lava brick script yay

local DB = script.Parent

function onTouched(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")

if hit.Name == "destructable" or hit.Parent:FindFirstChild("Humanoid") then
print 'part found'
hit:Destroy()
end
end

DB.Touched:Connect(onTouched)


I have tried GetTouchingParts, but it requires parts to have collision enabled, and the main destruction brick has collisions off. A players legs also have collision disabled which makes it pointless.

I suppose it's also worth noting the brick is constantly moving across the X plane; I'm not sure if that would affect anything in terms of scripting.

0
I'm not sure if this would do much in terms of its performance, it's just a shot in the dark so I'm only commenting. have you considered a region3? Speedmask 80 — 3d
0
I dabbled with it a little - although I'm not familiar with it so I'd need to learn more about it before I could use it. flamethower2002 50 — 3d

### 2 answers

Log in to vote
0
Answered by 3 days ago
Edited 3 days ago

a region3 basically gets two points, the bottom left corner and the top right corner, to make a cube which you can use to detect things. try this thing I cooked up.

local brickODoom = Instance.new("Part", workspace)
brickODoom.Position = Vector3.new(5, 5, 5)
brickODoom.Size = Vector3.new(10, 10, 10)
brickODoom.BrickColor = BrickColor.new("Really red")
brickODoom.Transparency = 0.75
brickODoom.Anchored = true
brickODoom.CanCollide = false

function translate(offset)
for i = 1, 30 do
brickODoom.Position = brickODoom.Position + offset
local region = Region3.new(brickODoom.Position - brickODoom.Size / 2, brickODoom.Position + brickODoom.Size / 2)
for _, part in pairs(workspace:FindPartsInRegion3(region)) do
if part.Name == "Destructible" or part.Parent:FindFirstChild("Humanoid") then
part:Destroy()
end
end
wait(0.1)
end
end

while true do
translate(Vector3.new(1, 0, 0))
translate(Vector3.new(-1, 0, 0))
end


you can try putting some bricks in front and see what happens.

your brick isn't actually doing anything, it's really the region3. the brick is just there to make it visible. you also can't move a region3 so by moving the brick, you can construct a new region3 around the brick every loop.

you said your brick is constantly moving so I did a check for parts every translation. if not, you can of course run a separate loop and scan for parts. scanning a region3 can also be pretty costly especially if there are a lot of parts, just like GetTouchingParts.

it's also worth mentioning that there is a method called FindPartsInRegion3WithWhiteList where you can put a list of parts you want to destroy. if possible, passing in all of your destructible parts as a table along with a list of players may improve the efficiency.

0
Okay awesome, I'll have to look into whitelists, but this is far more efficient at destruction than the script I made, thank you so much for your help! flamethower2002 50 — 3d
Log in to vote
0
Answered by 4 days ago
local DB = script.Parent

DB.Touched:Connect(function(hit)
if hit.Name == "destructable" then
print("Part found")
hit:Destroy()
hit:BreakJoints()
elseif hit.Parent:FindFirstChild("Humanoid") then
print("Player found")
hit:Destroy()
hit.Parent:BreakJoints()
end
end)


This script should work.

0
The script in the original post works fine, as mentioned; the problem is the fact that it's inconsistent and doesn't always destroy parts- flamethower2002 50 — 4d