Script can't check health value

I have an issue with my script but i don’t know what’s the problem/how to fix it
The script :

local miningevent = game:GetService("ReplicatedStorage"):WaitForChild("Mining")
local hv = script.Parent.Value

miningevent.OnServerEvent:Connect(function()
	script.Parent.Value -= 1
	print(script.Parent.Value)
end)

if hv == 0 or hv<0 then
	print("destroyed")
	script.Parent.Parent:Destroy()
end

the problem seems to be when the hv/health value is equal to 0 the model should be destroyed. But for some reason it doesn’t. There doesn’t seem to be any problem with the destroy function but the script can’t check the health value
Any solutions to this?

Your script only checks for the health once.

Instead do:

while true do
wait()
if hv == 0 or hv<0 then
	print("destroyed")
	script.Parent.Parent:Destroy()
end
end

it still doesn’t work
char limit

Where is your script located? d

Your script only checks for health once and not updating hv
Try to do this instead:

local miningevent = game:GetService("ReplicatedStorage"):WaitForChild("Mining")
local hv = script.Parent

miningevent.OnServerEvent:Connect(function()
	hv.Value -= 1
	print(hv.Value)
end)

hv:GetPropertyChangedSignal("Value"):Connect(function()
if hv.Value == 0 or hv.Value<0 then
	print("destroyed")
	hv.Parent:Destroy()
end
end)
1 Like

tysm it works now but there’s an error
Workspace.Rocks.Rock.Health.Script:6: attempt to index nil with 'Value' how would i fix that?

Make sure you changed:

local hv = script.Parent.Value

To:

local hv = script.Parent
1 Like

Shouldn’t the script be destroyed after destroying script.Parent.Parent?
if not then do this (and just not print because it’s destroyed after changing hv to 0):

miningevent.OnServerEvent:Connect(function()
if not hv then return end -- stop code if hv got destroyed
	hv.Value -= 1
	print(hv.Value)
end)

ty it works now
character limit

1 Like

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