Is there a more effective way to update values?

Trying to get my script to read the value of Current Stamina; I’ve tried using the .Changed event as of follows, but don’t seem to get it to work:

local CurrentStaminaEvent = game.ReplicatedStorage.Stats.CurrentStamina
local CurrentStamina = Player.leaderstats.CurrentStamina
local Stamina = Player.leaderstats.Stamina

CurrentStamina.Changed:Connect(function(NewValue)
	if NewValue - Stamina.Value <0 then
		local Amount = Stamina.Value*0.1
		repeat
			wait(5)
			local CurrentStamina = Player.leaderstats.CurrentStamina
			CurrentStaminaEvent:FireServer(Amount)
		until CurrentStamina.Value - Stamina.Value > 0
	end
end)

The issue with this script seems to be that NewValue is being read before currentStamina.Value, so it keeps looping even after CurrentStamina has exceeded Stamina.

On the other hand, i managed to get this to work:

while true do
	wait()
	CurrentStamina = Player.leaderstats.CurrentStamina
	if CurrentStamina.Value - Stamina.Value < 0 then
		repeat wait(5)
			local Amount = Stamina.Value*0.1
			CurrentStaminaEvent:FireServer(Amount)
			print("Stam fire server")
		until CurrentStamina.Value - Stamina.Value > 0
	end
end

Is there a more efficient way to get new values constantly without running a loop like a thousand times a minute?

.Changed returns the name of the value that changed, not the value itself. Use CurrentStamina[NewValue]

Alternatively use:
Instance:GetPropertyChangedSignal

Not entirely sure how this works with other values, but if the variable is a NumberValue is most definitely returns a number - I have it running this way on some of my code at the moment with have not run into any problems yet. Correct me if I’m misunderstanding.

Ignore this.


~~I believe you have the variables in the first script provided switched.

This like looks like the culprit, because I’m pretty sure you meant this:

if Stamina.Value - NewValue < 0 then

Does changing this work for you?

I believe it’s this line of code here:

local Amount = Stamina.Value*0.1 -- This is not updating like it does in the second script.
		repeat
			wait(5)
			local CurrentStamina = Player.leaderstats.CurrentStamina
			CurrentStaminaEvent:FireServer(Amount)
		until CurrentStamina.Value - Stamina.Value > 0

where the amount value is not updating like it does in the second code. Have you putting it in the repeat loop like this:

		repeat
			wait(5)
            local Amount = Stamina.Value*0.1 -- Here it is now.
			local CurrentStamina = Player.leaderstats.CurrentStamina
			CurrentStaminaEvent:FireServer(Amount)
		until CurrentStamina.Value - Stamina.Value > 0

You could hold a reference to the previous value changed when current stamina has changed. For example:

local CurrentStaminaEvent = game.ReplicatedStorage.Stats.CurrentStamina
local CurrentStamina = Player.leaderstats.CurrentStamina
local PrevStamina = CurrentStamina
local Stamina = Player.leaderstats.Stamina

CurrentStamina.Changed:Connect(function(NewValue)
	if NewValue - Stamina.Value <0 then
		local Amount = Stamina.Value*0.1
		repeat
			wait(5)
			local CurrentStamina = Player.leaderstats.CurrentStamina
			CurrentStaminaEvent:FireServer(Amount)
		until CurrentStamina.Value - Stamina.Value > 0
	end
    PrevStamina = NewValue
end)

Then you could use PrevStamina when .Changed occurs again to get the value before it was changed. Not sure what you’re trying to accomplish with the value so I’ll let you figure that out.