I have made a simple HeartBeat script that will change the brickcolor and material of any parts that enter it and destroy them afterwards. It works okay, but seems to break and just stop working if a large number of parts enter the Region3 zone simultaneously.

Ideally, I would want it to destroy parts whose parent is workspace the way it already does, but if the parts are in a model, then I would like the whole model to go in one, but only if that model is fully inside that Region3 zone rather than just touching it.

I tried adding an else and defining a local h as hit.Parent and a local t as h:FindFirstChildOfClass("Part") and using that to destroy the whole content of a model, but this just made it break faster, and so did just destroying h.

This is what my script currently looks like:

Void = script.Parent

local RunService = (game:FindService("RunService") or game:GetService("RunService"))

local Base = workspace:FindFirstChild("Baseplate")

local Min = Void.Minimum
local Max = Void.Maximum


if not Void then script:Destroy() end

local Range = Region3.new(Min.WorldPosition,Max.WorldPosition)
local Ignore = {Min,Max,Void,Base}


while RunService.Heartbeat:Wait() do
	if Void.Anchored == false then
		Void.Anchored = true
	end
	
	local PartsInRange = workspace:FindPartsInRegion3WithIgnoreList(Range,Ignore,math.huge)
		
	for _,hit in pairs(PartsInRange) do
		if hit then			
			wait(0.01)
			hit.Transparency = 0.5
			hit.BrickColor = BrickColor.new("Maroon")
			wait(0.01)
			hit.Transparency = 0.55
			hit.BrickColor = BrickColor.new("Crimson")
			hit.Material = "ForceField"
			wait(0.005)
			hit:Destroy()
			hit = nil
		end
	end
end

I've tried, but I can't identify what's causing it to stop working. Sidenote: The spacing of my script is correct, but won't paste in here right.

3 comments

0

If there's every an error, do some print debugging. See what parts print and what parts don't. Identify where it stops printing, and you'll usually find your error

0

Logic error, that is*

0

After doing this and messing around with the script, I found that the error was "Attempt to index nil with Transparency." Almost like destroy is somehow activating first, or the script is running on the part when its parent is nil?