Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

More reliable method than onTouched?

Asked by 4 years ago
Edited 4 years 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?

01-- lava brick script yay
02 
03local DB = script.Parent
04 
05function onTouched(hit)
06    local hum = hit.Parent:FindFirstChild("Humanoid")
07 
08    if hit.Name == "destructable" or hit.Parent:FindFirstChild("Humanoid") then
09        print 'part found'
10        hit:Destroy()
11        end
12    end
13 
14DB.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 661 — 4y
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 — 4y

2 answers

Log in to vote
0
Answered by
Speedmask 661 Moderation Voter
4 years ago
Edited 4 years 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.

01local brickODoom = Instance.new("Part", workspace)
02brickODoom.Position = Vector3.new(5, 5, 5)
03brickODoom.Size = Vector3.new(10, 10, 10)
04brickODoom.BrickColor = BrickColor.new("Really red")
05brickODoom.Transparency = 0.75
06brickODoom.Anchored = true
07brickODoom.CanCollide = false
08 
09function translate(offset)
10    for i = 1, 30 do
11        brickODoom.Position = brickODoom.Position + offset
12        local region = Region3.new(brickODoom.Position - brickODoom.Size / 2, brickODoom.Position + brickODoom.Size / 2)
13        for _, part in pairs(workspace:FindPartsInRegion3(region)) do
14            if part.Name == "Destructible" or part.Parent:FindFirstChild("Humanoid") then
15                part:Destroy()
View all 25 lines...

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 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
01local DB = script.Parent
02 
03DB.Touched:Connect(function(hit)
04    if hit.Name == "destructable" then
05          print("Part found")
06          hit:Destroy()
07              hit:BreakJoints()
08    elseif hit.Parent:FindFirstChild("Humanoid") then
09          print("Player found")
10              hit:Destroy()
11              hit.Parent:BreakJoints()
12    end
13end)

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 — 4y

Answer this question