Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my code causing a part to be destroyed?

I know this is a long piece of code but the only part that I think is affecting it is the bottom part. For whatever reason after my explosion occurs the explosive decides to get rid of itself. I can't find why this would be in my code and I need help.

sp=script.Parent

triggered=false

sp.Explode.Changed:connect(function()
	if sp.Explode.Value and not triggered then
		triggered=true
		wait(.1)
		game.Debris:AddItem(sp,.1)
		sp.Transparency=1
		local e=Instance.new("Explosion")
		e.Hit:connect(function(hit)
			if hit and hit.Parent~=nil then
				local et=hit:FindFirstChild("Explode")
				if et then
					if not et.Value then
						et.Value=true
					end
				end
				if hit.Parent:FindFirstChild("RobloxStamper") or hit.Parent:FindFirstChild("RobloxModel") then
					game:GetService("Debris"):AddItem(hit.Parent,10)
				end
			end
		end)
		local blastradius=1000
		if sp:FindFirstChild("Configuration") then
			blastradius=sp.Configuration.BlastRadius.Value
		end
		e.Position=sp.Position
		e.BlastPressure=2000000
		e.BlastRadius=blastradius
		e.Parent=game.Workspace
		wait(.1)
		sp.parent:Clone()
	end
end)


sp.Trigger.SourceValueChanged:connect(function(val)
	if val>.5 then
		sp.Explode.Value=true
	end
end)


2 comments

0

Lines, 9 and 21 call scheduled removals; the only thing I see contradicting is the removal of sp at line 9\

0

Lines, 9 and 21 call scheduled removals; the only thing I see contradicting is the removal of sp at line 9.

1 answer

In line 9 of your code you said

game.Debris:AddItem(sp,.1)

This line adds the script's parent to the Debris Service for 0.1 seconds. After that time runs out, the part will be removed.

Later you say

sp.parent:Clone()

but by that time, sp doesn't exist and returns as a nil value. I hope this helps you out. I don't have Roblox Studio open while typing this so I can't double check my answer, but this should work.

This discussion is preserved as it was. New answers, comments, and votes are closed.