Problem with changing number value

local pickaxe = script.Parent
local canmine = pickaxe.CanMine
local anim = pickaxe.Animation
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character
local animtrack
local humanoid
local ismining = false
local isactive = false
local animplaying = false
local pickaxehitbox = pickaxe.Pickaxepart.PickaxeHitbox

local function CharAdded(char)
	character = char
	humanoid = character:WaitForChild("Humanoid")
	animtrack = humanoid:LoadAnimation(anim)
end

if player.Character then CharAdded(player.Character) end
player.CharacterAdded:Connect(CharAdded)

canmine.Value = true	

pickaxe.Activated:Connect(function()
	if canmine.Value == true and animplaying == false then
		print("activated")
		isactive = true
		animtrack:Play()
		canmine.Value = false
		animplaying = true
		wait(3)
		animplaying = false
		pickaxehitbox.Touched:Connect(function(hit)
			if ismining == false then
				if hit.Name == "Hitbox" or hit.Parent == "Rocks" then
					print("mining")
					ismining = true
					local health = hit.Parent.Health
					local healthvalue = health.Value
					print(healthvalue)
					if health:isA("NumberValue") then
						healthvalue = healthvalue - 1
					end
					if ismining == true then
						wait(1)
						ismining = false
					end
				end
			end
		end)
		task.wait(1)
		canmine.Value = true
	end
end)

if isactive == true then
	wait(1)
	isactive = false
end

i want to change the health value by the mining function but for some reason it doesn’t work, im pretty sure it’s correct but can anyone correct me?

1 Like

This doesn’t work because when you access any property, it returns a copy of the object’s property, not the actual property itself. In this code you are just modifying a local value. Change that to this instead

local health = hit.Parent.Health
					if health:isA("NumberValue") then
					print(health.Value) -- prevent errors if health doesn't have value property
						health.Value -= 1 -- looks cleaner
					end

oh, thankyou i didn’t know that

oops sorry i made a grave mistake, i actually realized it still doesnt work because it still returns the value of the health which is 100 and it doesn’t -1 so is there any solution to this?
image

and im pretty sure i followed your exact script

i changed the value manually to see if it printed correctly and it perfectly does it
image

That’s not exactly what I wrote, replace the part of the script I mentioned entirely with the new script and don’t change anything, it will work. You modified a bit and started to change the replication of the value again


like this?

1 Like

oh thankyou! it works perfectly fine now, sry im so dumb lol

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.