RblxScriptSignal running multiple times for seemingly no reason?

Hello, I am making a game that features some objects to be breakable.

The issue is that the code, after 2 explosions, starts running exponentially(sort of?).
The goal is to have the code only run ONCE when an explosion is added.

it prints;

> blah -- 1st shot
> deleted
> blah -- 2nd shot
> deleted
> blah (x2) -- 3rd shot
> deleted
> blah (x7) -- 4th shot
> deleted
> blah (x51) -- 5th shot

Either the solution wasn’t clear(the code only matched the user’s and I didn’t see a difference), or the problem was not equal to mine.

I’ve had this connection problem many times in the past, and I can’t use rblxscriptsignal:Disconnect() on it because then the script wouldn’t do its job.(I am referring to the workspace.ChildAdded, not the explosion code.)

script below:

local fragment = require(script:WaitForChild("FragmentSmasher"))


workspace.ChildAdded:Connect(function(i) -- this repeats multiple times, I placed a print to check, it causes the rest of the code to multiply and lag the game out with many glass shards.
	if i:IsA("Explosion") then
		if i:HasTag("Break") then
			local hitExp = i.Hit:Connect(function(hit:BasePart)
				if hit:FindFirstChildOfClass("HingeConstraint") then
					local hinge = hit:FindFirstChildOfClass("HingeConstraint")
					if hinge:HasTag("Breakable") then
						hinge.Enabled = false
						task.wait(15)
						hinge.Enabled = true
					end
				end
				
				if hit:HasTag("Glass") then
					print("kabooey")
					local old = hit.Parent
					local replacement = hit:Clone()
					task.wait()
					local shards = fragment.BreakSurface(hit, hit.Position, (hit.Position-i.Position).Unit*(i.BlastPressure/150), "2978605361", 5)
					for k,j in pairs(shards:GetDescendants()) do
						if j:IsA("BasePart") then
							local damageScript = script:WaitForChild("fragmentPAIN"):Clone()
							damageScript:SetAttribute("damage", 10)
							damageScript.Parent = j
							damageScript.Enabled = true
						end
					end
					shards.Parent = old
					task.wait(5)
					replacement.Parent = old
					return
				end
			end)
			repeat task.wait()
			until not i:IsDescendantOf(workspace)
			hitExp:Disconnect()
			return
		end
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Try :Once() instead of :Connect()

1 Like

I will try this. I will let you know if this works

1 Like

this just causes the script to run once when my player loads in…

Wasn’t that the goal? I thought it was repeating

it’s supposed to always wait for an explosion, but it seems to fire more than once after 1 explosion is put into workspace

sorry if I did not explain properly.

I figured out, this only happens AFTER the explosion blows up an object(eg glass). shooting anything else that can’t be blown up does nothing.

FIXED, THIS WAS CAUSED BY THE FOLDER FOR SHARDS REMAINING(idk how tbh but its fixed)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.