:GetPropertyChangedValue() Doesnt register 1st time i use it

  1. What do you want to achieve?
    Trying to make mana regeneration and when i decrease my number of mana, i use :GetPropertyChangedValue() to detect when mana value changes, and then i set the bar’s size to mana.value / 100, Mana is a double constrained value.

  2. What is the issue?
    For some reason, first time it doesn’t detect it and the second time and so on it works.
    https://gyazo.com/df9d3f232578ba1f7937ba50f58f4ad8

  3. What solutions have you tried so far?
    Tried using other methods, in fact this is a different method and it still doesn’t work :frowning:

1 Like

Can you send us the script here? We can’t just find the problem based on only the video

2 Likes

If “Mana” is a value instance, you can use .Changed event, that also return current value.

1 Like

I forgot somehow, sorry about that. Here it is:

function functions.Mana(...)
	local things = ...;
	
	if not things then return end
	
	local Player : Player = things.Player
	local UserId = Player.UserId
	local Mana : DoubleConstrainedValue = Player.Mana
	local Amount = things.Amount
	
	local ManaBar = Player.PlayerGui["Magic System"].Mana.Fill
	
	Mana.Value -= Amount
	
	Mana:GetPropertyChangedSignal("Value"):Connect(function()
		ManaBar.Size = UDim2.fromScale(Mana.Value/100, 1)
	end)
	
	if not RegenState[UserId] then 
		RegenState[UserId] = {}
	end
	
	if RegenState[UserId] == true then return end
	RegenState[UserId] = true
	
	while Mana.Value < 100 do	
		task.wait(1)
		
		Mana.Value += 5
		
	end
	
	RegenState[UserId] = nil
end
1 Like

Until the event has been connected any changes won’t fire the event, so code in front of this won’t run first time.

Code after this should trigger the signal.
Although each time you call this function it would connect the event again, so it may be worth adding a disconnect here after the while loop has completed. Or use a separate function to connect the event and to update the mana.

1 Like

You can use a while loop instead :

local ManaBar = script.Parent.ManaBar
while true do
    -- Change your bar size to "mana.Value / 100"
    wait(0.01)
end
1 Like

How do you suggest i disconnect it? The first time the code wont run even though it changed from 100 to 95, second time and so on it works as expected. I dont know how to fix this, please help me

1 Like

Ok, try this:

function functions.Mana(...)
	local things = ...;
	
	if not things then return end
	
	local Player : Player = things.Player
	local UserId = Player.UserId
	local Mana : DoubleConstrainedValue = Player.Mana
	local Amount = things.Amount
	
	local ManaBar = Player.PlayerGui["Magic System"].Mana.Fill
	
	local connection = Mana:GetPropertyChangedSignal("Value"):Connect(function()
		ManaBar.Size = UDim2.fromScale(Mana.Value/100, 1)
	end)
	Mana.Value -= Amount --moved to after the event has been connected 
	if not RegenState[UserId] then 
		RegenState[UserId] = {}
	end
	
	if RegenState[UserId] == true then return end
	RegenState[UserId] = true
	
	while Mana.Value < 100 do	
		task.wait(1)
		
		Mana.Value += 5
		
	end
    if connection then --check connection still exists
	connection:Disconnect() --when loop ends this will run
    end
	RegenState[UserId] = nil
end