Log chopping script sometimes spitting out this odd error

Error:
11:09:27.351 - ServerScriptService.Script:28: attempt to index nil with ‘Value’

11:09:27.352 - Stack Begin

11:09:27.352 - Script ‘ServerScriptService.Script’, Line 28

11:09:27.353 - Stack End

Here is my server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local log = ReplicatedStorage.WoodLog

local debounce = false
local swinging = false

local chop1 = Instance.new("Sound")
chop1.SoundId = "rbxassetid://159798328"
local woodBreak = Instance.new("Sound")
woodBreak.SoundId = "rbxassetid://4988580646"

ReplicatedStorage.Remotes.ChopTree.OnServerEvent:Connect(function(player)
	if debounce then return end
	
	local character = player.Character
	
	if character:FindFirstChild("Axe") then
		debounce = true
		swinging = true
		
		if swinging then
			local connection
			connection = character:FindFirstChild("Axe").Touched:Connect(function(hit)
				if not swinging then return end
				if swinging then
					if hit.Name == "Stump" then
						local hits = hit:FindFirstChild("Hits")
						if hits.Value >= 3 then -- this is line 28
							local logClone = log:Clone()
							logClone.Anchored = false
							logClone.CanCollide = true
							logClone.Parent = workspace
							logClone.Position = hit.Position
							local woodBreakClone = woodBreak:Clone()
							woodBreakClone:Clone()
							woodBreakClone.Parent = hit
							woodBreakClone.PlayOnRemove = true
							woodBreakClone:Play()
							woodBreakClone:Destroy()
							hit.CanCollide = false
							hit.Parent:FindFirstChild("Leaf"):Destroy()
							hit:Destroy()
							wait(.4)
							logClone.Anchored = true
							connection:Disconnect()
						else
							local chop1Clone = chop1:Clone()
							chop1Clone.Parent = hit
							chop1Clone:Play()
							hits.Value = hits.Value + 1
							connection:Disconnect()
						end
					end
				end
			end)
		end
		local axeSwing = character.Humanoid:LoadAnimation(script.AxeSwing)
		axeSwing:Play()
		character.Humanoid.WalkSpeed = 6
		axeSwing.Stopped:Wait()
		character.Humanoid.WalkSpeed = 16
		debounce = false
		swinging = false
	end
	
end)

(If you saw my post from the other day I kinda forgot about FE and partly re-wrote the script.)

Somehow you aren’t getting hits correctly, like it can’t find first child “hits”.

hit:FindFirstChild("Hits") is nil. I guess some of your stumps don’t have something called “Hits” in them.

2 Likes

Turns out the event was firing very quickly, so to counteract that I simply set a value to false as soon as it’s true.

What I did:

connection = character:FindFirstChild("Axe").Touched:Connect(function(hit)
if not swinging then return end
swinging = false -- this is the thing that "fixed" it
if hit.Name == "Stump" then
local hits = hit:FindFirstChild("Hits")
if hits.Value >= 3 then

-- rest of code