Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Mr_Unlucky 1085 Moderation Voter
5 years ago

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()
0
Please post the full output error. User#19524 175 — 5y
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. mattscy 3725 — 5y
0
@incapaz attempt to index field 'Parent' ( a nil value ) Mr_Unlucky 1085 — 5y
0
Matt, how can I fix it then? Mr_Unlucky 1085 — 5y

1 answer

Log in to vote
0
Answered by
CjayPlyz 643 Moderation Voter
5 years ago
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.

Ad

Answer this question