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 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)
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)