So I have certain objects in my game that are periodically placed. I’m trying to have my script destroy the object once its taken enough damage. It works, however after being destroyed I’m still getting errors in my output like
14:00:42.527 Workspace.Wall.selfdelete:16: attempt to index nil with 'States' - Studio
Is there a way to completely stop the script as its being destroyed?
local damage = 0
script.Parent.States.iFrames.Changed:Connect(function()
damage = damage + 1
if damage == 1 then
script.Parent.HumanoidRootPart.front.Transparency = 0.8
script.Parent.HumanoidRootPart.back.Transparency = 0.8
elseif damage == 2 then
script.Parent.HumanoidRootPart.front.Transparency = 0.2
script.Parent.HumanoidRootPart.back.Transparency = 0.2
elseif damage == 3 then
script.Parent:Destroy()
script:Destroy()
end
print("Wall damaged")
wait(1)
if script.Parent.States.iFrames.Value~=0 then
script.Parent.States.iFrames.Value=0
end
end)
If you call script.Parent:Destroy() and script:Destroy() in lines 11 & 12 then how can you call if script.Parent... in line 16?
If you are destroying the Parent why would you be trying to set iFrames.Value? The Parent wouldn’t exist.
Try leaving out those last lines (put – in front so if there’s no change it’s easy to change back).
Or how about after the wait(1) you put if script.Parent.States == nil then return end