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

Why won't my script destroy the parts when it touches it?

Basically, I'm building a nuke where when it detonates it destroys any parts in it's radius, but if it's a player they lose HP. However, it's not working and instead leaves the parts it's supposed to destroy and destroys any parts that it comes in contact afterwards. The error says that I'm attempting to index field parent, because it's a nil value. Any help?

local explosion = Instance.new("Part",workspace)
explosion.Position = script.Parent.thing.Position
wait(0.1)

explosion.Shape = "Ball"
explosion.BrickColor = BrickColor.new("Bright green")
explosion.Material = "Foil"
explosion.CanCollide = false
explosion.Anchored = true
explosion.Transparency = 0.25

for i = 5,0,-1 do
	explosion.Size = explosion.Size * 2
	wait(0.3)
	explosion.Touched:Connect(function(touch)
		if touch.Parent:FindFirstChild("Humanoid") then
			touch.Parent.Humanoid:TakeDamage(100)
		else
			touch:Destroy()
		end
	end)
end

script.Parent.Nuke:Destroy()

4 comments

0

Please post the full output error.

0

After you :Destroy() the part, it's parent will become nil, so you can no longer refer to the parent of 'touch' in the script after line 19 occurs.

0

@incapaz attempt to index field 'Parent' ( a nil value )

0

Matt, how can I fix it then?

1 answer

local explosion = Instance.new("Part")
explosion.Shape = "Ball"
explosion.BrickColor = BrickColor.new("Bright green")
explosion.Material = "Foil"
explosion.CanCollide = false
explosion.Anchored = true
explosion.Transparency = 0.25
explosion.Position = script.Parent.thing.Position
wait(0.1)

for i = 1, 5 do
	explosion.Size = explosion.Size * 2
	explosion.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				hit.Parent.Humanoid:TakeDamage(100)
			else
				hit:Destroy()
			end
		end
	wait(0.3)
end)

script.Parent.Nuke:Destroy()

i don't see anything wrong, by the way @mattscy after line 19 it will still work because of line 15.

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