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
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