Ignoring if statement

So I am making a pickaxe for my survival game, whenever you hit the piece of stone with a pickaxe it will make a value named “Hits” go down by one, then an if statement is stated saying if the value is 0 it checks for the type value and goes with that info.

local tool = script.Parent.Parent
local axeDet = script.Parent
local debounce = false

tool.Equipped:Connect(function()
	axeDet.Touched:Connect(function(hit)
		
		if hit.ClassName == "Part" and hit.Name == "Stone" or hit.Name == "Ore" then
			print("it was stone")
			if debounce == false then
				print("good to go")
				local hits = hit.Parent.Hits
				if hits then
					hits.Value = hits.Value - 1
					print("hit down")


					if hits.Value == 0 then
						local typee = hit.Parent.Type
						local humanoid = tool.Parent.Humanoid
						local plr = game.Players:GetPlayerFromCharacter(humanoid.Parent)
						if typee.Value == "Stone" then
							plr.hiddenStats.hasStone.Value = true
							plr.hiddenStats.stoneCount.Value = plr.hiddenStats.stoneCount.Value + 1
							hit.Parent:Destroy()
							print("destroyed")
						elseif typee.Value == "Steel" then
							plr.hiddenStats.hasStone.Value = true
							plr.hiddenStats.steelCount.Value = plr.hiddenStats.steelCount.Value + 1
							hit.Parent:Destroy()
							print("destroyed")
						elseif typee.Value == "Gold" then
							plr.hiddenStats.hasStone.Value = true
							plr.hiddenStats.goldCount.Value = plr.hiddenStats.goldCount.Value + 1
							hit.Parent:Destroy()
							print("destroyed")
						elseif typee.Value == "Coal" then
							plr.hiddenStats.hasStone.Value = true
							plr.hiddenStats.coalCount.Value = plr.hiddenStats.coalCount.Value + 1
							hit.Parent:Destroy()
							print("destroyed")
						end
					end
				end
				end
			else
				print("not enough hits")
				return
			end
		debounce = true
		wait(3)
		debounce = false
	end)
end)

It just keeps going into the negatives, and does not return an error, what do I do?

2 Likes

To prevent this, you could wrap your hit value decrease line in a if statement that checks to make sure that hits is greater than or equal to 0.

if hits.Value >= 0 then
   hits.Value = -= 1
end
1 Like

He is checking for when hits.Value == 0, so you would have to change the >= sign to just >.

The problem is that he is decreasing the hits value outside of a if statement that checks for the value being not less than one, so the number will go up regardless of the current value. That’s why he needs the if statement I recommended.

1 Like

Yeah I agree with what you’re saying.

if hits then
    if hits.Value > 0 then
        hits.Value -= 1
    else
        --Code for destroying the stone
     end
end
1 Like

This is what I put for it and it says on line #14 it is comparing a number and string?

local tool = script.Parent.Parent
local axeDet = script.Parent
local debounce = false

tool.Equipped:Connect(function()
	axeDet.Touched:Connect(function(hit)
		
		if hit.ClassName == "Part" and hit.Name == "Stone" or hit.Name == "Ore" then
			print("it was stone")
			if debounce == false then
				print("good to go")
				local hits = hit.Parent.Hits
				if hits then
					if hits.Value > 0 then
						hits.Value -= 1
					else
						local typee = hit.Parent.Type
						local humanoid = tool.Parent.Humanoid
						local plr = game.Players:GetPlayerFromCharacter(humanoid.Parent)
						if typee.Value == "Stone" then
							plr.hiddenStats.hasStone.Value = true
							plr.hiddenStats.stoneCount.Value = plr.hiddenStats.stoneCount.Value + 1
							hit.Parent:Destroy()
							print("destroyed")
						elseif typee.Value == "Steel" then
							plr.hiddenStats.hasStone.Value = true
							plr.hiddenStats.steelCount.Value = plr.hiddenStats.steelCount.Value + 1
							hit.Parent:Destroy()
							print("destroyed")
						elseif typee.Value == "Gold" then
							plr.hiddenStats.hasStone.Value = true
							plr.hiddenStats.goldCount.Value = plr.hiddenStats.goldCount.Value + 1
							hit.Parent:Destroy()
							print("destroyed")
						elseif typee.Value == "Coal" then
							plr.hiddenStats.hasStone.Value = true
							plr.hiddenStats.coalCount.Value = plr.hiddenStats.coalCount.Value + 1
							hit.Parent:Destroy()
							print("destroyed")
						end
					end
				end
			end
		end
		debounce = true
		wait(3)
		debounce = false
	end)
end)

Is “hits” a StringValue or a Number/IntValue?

It is an Int Value although there is a StringValue next to it called Type.

It’s still the same error, that didn’t do anything to it.

The error is saying it is on line 14 and you are comparing a string and a number, right?

Isn’t this line 14:

If so, that means that the “hits” it is referencing must be a string. Try to make sure that you are indexing the right value.

I restarted studio the values must’ve glitched, now when I hit it, it stays at 1.

Oh make sure it is this code:

hits.Value -= 1
2 Likes