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.