Making a health system for an object in my game, I got the intvalue done but the problem is the surface gui that show the health is not changing properly
as you can see when I change the value the text gui use the old value not the new one. Here’s my script
local health = script.Parent.Health
local explosion = script.Parent.Under.Plate.Explosion
local Microwave = script.Parent
health.Changed:Connect(function()
if health.Value > 0 then
local healthvalue = script.Parent.Health.Value
script.Parent.Control.Health.SurfaceGui.Background.TextLabel.Text = healthvalue.."/500"
end
end)
local health = script.Parent.Health
local explosion = script.Parent.Under.Plate.Explosion
local Microwave = script.Parent
health.Changed:Connect(function()
wait() -- ADD WAIT HERE
if health.Value > 0 then
local healthvalue = script.Parent.Health.Value
script.Parent.Control.Health.SurfaceGui.Background.TextLabel.Text = healthvalue.."/500"
end
end)
Try adding a wait? It seems to me like it is just not updating fast enough
This is a weird issue, but I would suggest not making a new variable (healthvalue) when you already declared it in variable health. Try this:
local health = script.Parent.Health
local explosion = script.Parent.Under.Plate.Explosion
local Microwave = script.Parent
health.Changed:Connect(function()
if health.Value > 0 then
script.Parent.Control.Health.SurfaceGui.Background.TextLabel.Text = health.Value.."/500"
end
end)
That script should work fine at a glance. I think we should do some basic debugging to see where the problem is. I recommend adding various print statements so we can see where the script runs, running the script, and then checking the output log, like so:
local health = script.Parent.Health
local explosion = script.Parent.Under.Plate.Explosion
local Microwave = script.Parent
print("Script ran.")
health.Changed:Connect(function()
print("Value changed.")
if health.Value > 0 then
script.Parent.Control.Health.SurfaceGui.Background.TextLabel.Text = health.Value.."/500"
end
end)
This will help us know where exactly our problem lies. If both print statements work, then we can go from there. If not, then the problem will be far easier to diagnose.
Also, if it’s not too much trouble, make sure that there aren’t any errors in your output. Sometimes they can be easy to overlook.