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

How do i find and delete all parts of a certain name in a region?

Asked by 3 years ago
Edited 3 years ago

I'm making a script for a building game which can be grieffed very easily by spamming balls at spawn. Sadly, this creates so much lag that deleting the balls by hand is very slow and time consuming. I do not intend on adding any anti-spam feature on the placement tool, but to make a function that clears all balls in the proximity of the moderator who started that function.

While creating this script, I ran into the problem of FindPartsInRegion3WithWhitelist only outputting one instance, but quickly fixed it using a while loop. While this fixed the problem, it also created the issue of the script not working if a ball was outside of the target region.

Whats the most efficient method to fix this script / do what im trying to do?

Here's my code:

--This remote event fires when "Clear all balls in proximity" button is pressed
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
    deleteBalls()
end)

--This region would be the player's proximity
local point1 = Vector3.new(-10, -10, -10)
local point2 = Vector3.new(10, 10, 10)
local region = Region3.new(point1, point2)

--Find every part that is 'workspace.Ball' and delete them until there are none left.
function deleteBalls()
    local parts = workspace:FindPartsInRegion3WithWhiteList(region, {workspace.Ball}, 1) 
    while #parts > 0 do
        parts[1]:Destroy()
        print("Ball destroyed!")
        parts = workspace:FindPartsInRegion3WithWhiteList(region, {workspace.Ball}, 1)
    end
end
0
Instead of having a region where the balls are to be deleted, what if when you create a ball, it only works if it is inside a region. SteamG00B 1633 — 3y
0
That or use the debris service so they despawn after about 30 seconds. SteamG00B 1633 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

This should work:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
    DeleteBalls()
end)


local point1 = Vector3.new(-10, -10, -10)
local point2 = Vector3.new(10, 10, 10)
local region = Region3.new(point1, point2)


function DeleteBalls()
    local RegionG = game.Workspace:FindPartsInRegion3(region,nil,100)
    for i, v in pairs(RegionG) do
        if v.Name == "Ball" then
            v:Destroy()
            print("A ball was destroyed.")
        else
        end
    end
end
0
This technically works, but I'd have to raise maxParts because there is usually around 100 parts around the player at all times. mathieuqwe 2 — 3y
Ad

Answer this question