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

how to fix this Roblox Atempt to index nil value error ?

=Problem= Script Errors On Line 11; Attempt to index nil Value

=Script=

deb = false
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=713970194"
animTrack = script.Parent.Parent.Parent.Parent.Humanoid:LoadAnimation(animation)


function onDamage(Part)
	print('fired') -- prints
    if Part.Parent:FindFirstChild("Humanoid") ~=nil and script.Parent.Parent.Parent.Parent.Swinging.Value == true and deb == false or Part.Parent.Parent:FindFirstChild("Humanoid") ~=nil and deb == false then
		deb = true
		if Part.Parent:FindFirstChild("Blocking").Value == true or 			Part.Parent.Parent:FindFirstChild("Blocking").Value == true then -- error's here
			print('play')
			animTrack:Play()			
			Part.Parent:FindFirstChild("Blocking").Value = false		
		else
		Part.Parent.Humanoid:TakeDamage(script:WaitForChild('Damage').Value)
		print('play')
		wait(.5)		
		deb = false

		end
    end
end
script.Parent.Touched:connect(onDamage)

1 answer

✓ Accepted answer

With how you set up the if__or__then statement, you allowed for a situation where it could try to index the Value of a nil type.

Specifically, look at it like this:

if Part.Parent:FindFirstChild("Blocking").Value == true or          Part.Parent.Parent:FindFirstChild("Blocking").Value == true then

When Lua compiles this and runs it, it'll first try to find that instance named 'Blocking'. If it finds it, awesome. Then it can simply try to index the property 'Value' and run it. But if there isn't an instance named 'Blocking', it'll return nil, which means that the code now looks more like if nil.Value == true then. Remember that 'nil' is a type and doesn't have a property named 'Value'. Thus, you get an error.

To fix your code, you need to get the Instance's before you check their values. I've fixed it as shown below:

deb = false
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=713970194"
animTrack = script.Parent.Parent.Parent.Parent.Humanoid:LoadAnimation(animation)


function onDamage(Part)
    print('fired') -- prints
    if Part.Parent:FindFirstChild("Humanoid") ~=nil and script.Parent.Parent.Parent.Parent.Swinging.Value == true and deb == false or Part.Parent.Parent:FindFirstChild("Humanoid") ~=nil and deb == false then
        deb = true
	local Val = Part.Parent:FindFirstChild("Blocking") or Part.Parent.Parent:FindFirstChild("Blocking")
        if Val and Val.Value == true then
            print('play')
            animTrack:Play()            
            Val.Value = false        
        else
       	Part.Parent.Humanoid:TakeDamage(script:WaitForChild('Damage').Value)
        print('play')
        wait(.5)        
        deb = false
        end
    end
end
script.Parent.Touched:connect(onDamage)

Hope this helped! -Poe

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