How can I make the script update with the latest value?

  1. What do you want to achieve? I want the script to update the gui with the latest value of magic

  2. What is the issue? not sure how to use .changed()

  3. What solutions have you tried so far? I did look on the devHub but it’s not very useful-

if you have any questions, let me know

local player = game.Players.LocalPlayer
local character = player.Character
local witch = character:WaitForChild("Witch")
local magic = character:WaitForChild("Magic")
local magicCapacity = character:WaitForChild("MagicCapacity")
local main = script.Parent.Frame.Main
local MagicGui = script.Parent
local Text = script.Parent.Frame.TextLabel
local percent = magic.Value / magicCapacity.Value

if witch.Value == true then
	MagicGui.Enabled = true
	Text.Text = magic.Value.."/"..magicCapacity.Value
	main.Size = UDim2.new(percent,0,1,0)
	
end

should I use .Changed or some other function to get the latest value?

So you want the gui to update everytime the magic value is changed?
If so then.

magic.Changed:Connect(function()
	if witch.Value == true then
		MagicGui.Enabled = true
		Text.Text = magic.Value.."/"..magicCapacity.Value
		main.Size = UDim2.new(percent,0,1,0)
	end
end)
2 Likes

:Changed detects whenever any property of an instance changes, using :GetPropertyChangedSignal is more efficient as you can detect one property.

magic:GetPropertyChangedSignal("Value"):Connect(function()
	if witch.Value == true then
		MagicGui.Enabled = true
		Text.Text = magic.Value.."/"..magicCapacity.Value
		main.Size = UDim2.new(percent,0,1,0)
	end
end)

thank you so much! I’ll try it out and mark it as a solution if it works <3

The text shows up as Label, I’ve tried to use the other script above but it still shows up as Label. What else can I try since I’ve tried putting it under witch.Value yet it still didn’t work

Would you mind screen-shotting the output for when the value changes?

I actually figured it out, sorry for that but thank you so much for your help

If OP is using a ValueBase object then it’s not necessary to use GetPropertyChangedSignal. ValueBase objects have a specialised version of Changed that fires when the value changes and the new value is passed as an argument to connected functions. GetPropertyChangedSignal and Changed on a ValueBase should be functionally equivalent, but it is better to use Changed because you can access the new value as a passed value.

2 Likes