Attempt to index nil with 'Value'

I wanted to make an axe thingy like some survival game or smthing like that,but when it hits a resource it says “attempt to index nil with ‘Value’” on line 55 (its marked in the code),how can i fix it?

Here’s the script:

local Tool = script.Parent
local Handle = Tool.Handle
local ToolMotor6D = Handle.HandlePart:WaitForChild("Motor6D")
local HandlePart = Handle.HandlePart

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AnimationFolder = ReplicatedStorage.Animations

local debounce = false
local hitdebounce = false


local RESOURCEDAMAGE = 10
local PLAYERDAMAGE = 5


--// Animations //
local Swing = AnimationFolder.HatchetSwing




--// Animations //


Tool.Equipped:Connect(function()
	local Humanoid = Tool.Parent:FindFirstChild("Humanoid")
	
	
	ToolMotor6D.Part0 = HandlePart
	ToolMotor6D.Part1 = Tool.Parent:FindFirstChild("Right Arm") -- so the axe would actually be swung instead of only the character doing the movement
	
end)

Tool.Activated:Connect(function()
	
	
	local Humanoid = Tool.Parent.Humanoid
	local SwingLoaded = Humanoid:LoadAnimation(Swing)
	
	
	
	if debounce ~= true then -- if debounce is not active
		debounce = true
		
		SwingLoaded:Play()
		TouchEvent = Handle.Touched:Connect(function(hit) -- global so i could disconnect it on hit
			if hit then
				if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= Tool.Parent.Name then -- if the hit is a player and not the one that holding the weapon
					local EnemyHumanoid = hit.Parent.Humanoid
					
					EnemyHumanoid:TakeDamage(PLAYERDAMAGE)
					TouchEvent:Disconnect()
					
				-- Error	
				elseif hit.Parent:FindFirstChild("IsAResource") or hit:FindFirstChild("IsAResource") and hit.Parent.Name ~= Tool.Parent.Name then -- if the hit is a resource
					hit.Parent:FindFirstChild("HP").Value = hit.Parent:FindFirstChild("HP").Value - RESOURCEDAMAGE
					TouchEvent:Disconnect()
				end
				-- Error
				
			else -- if no hit
				
			end
		end)
		SwingLoaded.Stopped:Wait()
		TouchEvent:Disconnect()

		debounce = false
	else -- if debounce == true
		
	end
end)

The “Layout” of the tool:

and the “Layout” of the resource part:

Thanks in advance.

“hit:FindFirstChild(“IsAResource”) and hit.Parent.Name ~= Tool.Parent.Name” is probably what’s causing the error. hit.Parent:FindFirstChild(“HP”) will only find HP if hit is a part inside of the resource, but that “or” in your if check also allows the resource itself through.

The HP is in the resource,maybe i didnt understand what you said though.

“hit” is the “Resource” part. You’re doing hit.Parent:FindFirstChild(“HP”), meaning you’re not actually searching for HP in resource, but it’s parent.

2 Likes

I just took a look at my code again and realized that im dumb,thank you.