Change GUI Text Value when Clicked

I am very new to lua and scripting as a whole and doing my best to learn on my own so go easy on me, thank you.

I am looking to make a Healthbar GUI, that when clicked will go down by a factor of 1. I have already worked out the bar itself shrinking in size, but i also want to have the health amount in numbers go down when clicked, i.e. 100 click 99 click 98 click 97… and so on

I am having trouble actually getting this value to count down every time I click. I have provided an example of my script so far below:

local mob = game.Workspace.Mob
local clickDetector = game.Workspace.Mob.ClickDetector
local healthNumber = game.Workspace.Mob.healthbargui.barbackground.healthamount
local healthBar = game.Workspace.Mob.healthbargui.barbackground.bar
local healthValue = Instance.new("IntValue")
healthValue.Value = 100

function onMouseClick()
	healthBar.Size = healthBar.Size - UDim2.new(0.01, 0, 0, 0)
	print("Attack confirm")
	
	healthNumber = healthValue.Value - 1
	print("damage confirmed")
end

clickDetector.MouseClick:Connect(onMouseClick)

again as shown in the example above, i tried something i saw somehwere else on the forum, but only had minimal luck making it go from 100 to 99, after that it would stay at 99 even when clicked. Now it just seems to not even be subtracting at all. Im a bit stumped as to how i get the value to change with every click and then transfer that to the text value of the GUI

Thank you for your help

1 Like

Is healthNumber a value? If so. I think you need to use healthNumber.Value = healthValue.Value - 1 Make sure that you’re also updating the Gui everytime the Value changes.

healthNumber calls the Text GUI. I got it to work again, at least doing the 100 to 99, but how do I update the GUI every time the value changes? i know it has to do with a loop but i just dont know exactly how to structure the loop :confused:

Set healthValue to the players health so that way it works!

You don’t need to indeed use a loop - Yes, You can use one but it’s not worth. You should only update it whenever the value changes. This can be done via:

healthNumber:GetPropertyChangedSignal("Value"):Connect(function()
     -- Update function goes here
end)

You’re not updating healthValue so it remains 100 throughout and you’ll only get 100-1=99.

You need to do,

healthValue -= 1
HealthNumber = healthValue
1 Like

would it look like this?

function onMouseClick()
	healthBar.Size = healthBar.Size - UDim2.new(0.01, 0, 0, 0)
	print("Attack confirm")
	
	healthNumber:GetPropertyChangedSignal("Value"):Connect(function()
		healthNumber.Text = healthValue.Value - 1
		print("damage confirmed")
	end)

end

sorry, i am just a bit confused on where everything goes

Try changing it to the one below. You shouldn’t create a Connection everytime the Function gets fired since it is very expensive (laggy) and can bring alot of problems. Try using this code below:

-- Assuming you have all the variables set above...
-- Connect before the function
healthValue:GetPropertyChangedSignal("Value"):Connect(function()
	healthNumber.Text = healthValue.Value
	print("damage confirmed")
end)

function onMouseClick()
	-- Subtract healthValue by 1 and Change the healhBar Size
	healthValue.Value -= 1
	healthBar.Size -= UDim2.new(0.01, 0, 0, 0)
	print("Attack confirm")
end
1 Like

That seems to work, I will definetly look over how this works so i can understand it better, thank you for your help!!

2 Likes