What is wrong with my stats script?

This script is meant to make a currency textlabel that quickly increases numerically as a players currency goes up and goes down quickly (but visibly for both) as players currency goes down however if I increase my currency it goes up fine, if I decrease it after that it infinitely goes up(only visually in the textlabel since the stats increase like they should) and if I increase my currency after decreasing it it infinitely goes down.

Thanks to @Qxest for helping with formatting. Greatly appreciated! (Still don’t know if I did it to his standard but I tried.)

 ChangingValue = "Waiting"

 	local Remote = game:GetService("ReplicatedStorage").RemoteEvents:WaitForChild("Currency") --Explanatory 

	 Remote.OnServerEvent:Connect(function(player)
	
 	local CashText = player.PlayerGui.ScreenGui.TextLabel -- The textlabel that displays the sum of the currency.
   	local Amount = player.PlayerGui.Currency_Control.Currency.PreviousCurrency -- Not previous currency(will rename), was going to be previous, is the sum of currency this script uses.
   	local Prev = player.PlayerGui.Currency_Control.CurrencyLog.Log1 --A log that updates every 0.01 seconds storing the previous sum of currency the user had

   	player.Cash.Changed:Connect(function(NewValue)

   				if player.Cash.Value >= Prev.Value and ChangingValue == "Waiting" then --I tried this instead of boolean just to see I am aware Boolean is more efficient but got desperate
				--means nothing is happening in the script right now.
			ChangingValue = "Adding" --When the value is increasing change the ChangingValue to "adding" in order to clarify that
    repeat
    		Amount.Value = Amount.Value + 1
    	wait()
    		CashText.Text = ""..Amount.Value..""
		       wait(.000001) --Extremely fast wait time since it's not there for efficiency, it makes sure the textlabel catches up with their real sum of money relatively quickly. 
--I'll be changing this probably soon so the speed it catches up by depends on the sum of money that it's increasing by.

    	until
    		Amount.Value == player.Cash.Value 
    		ChangingValue = "Waiting" --Same as before
    		CashText.Text = ""..player.Cash.Value.."" --The sum of 'cash' the player has, temporary name for it.

    		elseif player.Cash.Value <= Prev.Value and ChangingValue == "Waiting" then

    	ChangingValue = "Subtracting" --I was tired so why the hell did I call this minusing in the previous version?
	        		repeat
    	Amount.Value = Amount.Value - 1
    wait()
    	CashText.Text = ""..Amount.Value..""
	    wait(.000001) --same as before

    until

    	Amount.Value == player.Cash.Value --The amount as explained before, clarifying it will stop when it hits the same sum as the cash.
			CashText.Text = ""..player.Cash.Value.."" --Updates to the text to whatever the cash value is just in-case there was some issue and the label isn't displaying it right.
			ChangingValue = "Waiting"--same as before
					elseif ChangingValue == "Adding" or ChangingValue == "Subtracting" then
			print("Do nothing") --This stops spam and was an attempt at stopping the bug I'm dscriving. Failed.

   end
   end)
   end)
1 Like

The code is running perfectly fine with this method the only issue is it won’t stop if I try and take away currency after adding it and vice versa. I’ll be sure to format my code better in the future

Try something like this:

ChangingValue = "Waiting"

local Remote = game:GetService("ReplicatedStorage").RemoteEvents:WaitForChild("Currency")

Remote.OnServerEvent:Connect(function(player)

local CashText = player.PlayerGui.ScreenGui.TextLabel

local Amount = player.PlayerGui.Currency_Control.Currency.PreviousCurrency

local Prev = player.PlayerGui.Currency_Control.CurrencyLog.Log1

local ValueChange = player.PlayerGui.ScreenGui.Room_Change.Control.Value

player.Cash.Changed:Connect(function(NewValue)

if player.Cash.Value >= Prev.Value and ChangingValue == "Waiting" then

ChangingValue = "Adding"

while Amount.Value ~= player.Cash.Value do

Amount.Value = Amount.Value + 1

wait(0.1)

CashText.Text = ""..Amount.Value..""

wait(0.1)

end

Amount.Value == player.Cash.Value

ChangingValue = "Waiting"

CashText.Text = ""..player.Cash.Value..""

elseif player.Cash.Value <= Prev.Value and ChangingValue == "Waiting" then

ChangingValue = "Minusing"

repeat

Amount.Value = Amount.Value - 1

wait()

CashText.Text = ""..Amount.Value..""

wait(0.1)

until

Amount.Value == player.Cash.Value

CashText.Text = ""..player.Cash.Value..""

ChangingValue = "Waiting"


elseif ChangingValue == "Adding" or ChangingValue == "Minusing" then

print("Do nothing")

end

end)

end)

This post was flagged, even though there are changes here…
Anyhow, don’t you repeat and until, use while.

1 Like

There’s still the bug that causes it to glitch out when it receives an opposite sum.
So basically, if I add cash and after that I take away cash the textlabels text will continue going up and up and won’t stop, if I take away cash and after that I add cash the textlabels text will continue decreasing

1 Like

Oh boy, let’s see…

Reformatted for Readability


ChangingValue = "Waiting"
local Remote = game:GetService("ReplicatedStorage").RemoteEvents:WaitForChild("Currency") --Explanatory 

Remote.OnServerEvent:Connect(function(player)
 	local CashText = player.PlayerGui.ScreenGui.TextLabel -- The textlabel that displays the sum of the currency.
   	local Amount = player.PlayerGui.Currency_Control.Currency.PreviousCurrency -- Not previous currency(will rename), was going to be previous, is the sum of currency this script uses.
   	local Prev = player.PlayerGui.Currency_Control.CurrencyLog.Log1 --A log that updates every 0.01 seconds storing the previous sum of currency the user had

   	player.Cash.Changed:Connect(function(NewValue)
   		if player.Cash.Value >= Prev.Value and ChangingValue == "Waiting" then --I tried this instead of boolean just to see I am aware Boolean is more efficient but got desperate
			--means nothing is happening in the script right now.
			ChangingValue = "Adding" --When the value is increasing change the ChangingValue to "adding" in order to clarify that
			repeat
				Amount.Value = Amount.Value + 1
				wait()
				CashText.Text = ""..Amount.Value..""
				wait(.000001) --Extremely fast wait time since it's not there for efficiency, it makes sure the textlabel catches up with their real sum of money relatively quickly. 
				--I'll be changing this probably soon so the speed it catches up by depends on the sum of money that it's increasing by.
			until Amount.Value == player.Cash.Value 

			ChangingValue = "Waiting" --Same as before
			CashText.Text = ""..player.Cash.Value.."" --The sum of 'cash' the player has, temporary name for it.
		elseif player.Cash.Value <= Prev.Value and ChangingValue == "Waiting" then
			ChangingValue = "Subtracting" --I was tired so why the hell did I call this minusing in the previous version?
	        repeat
    			Amount.Value = Amount.Value - 1
    			wait()
    			CashText.Text = ""..Amount.Value..""
	    		wait(.000001) --same as before
    		until Amount.Value == player.Cash.Value --The amount as explained before, clarifying it will stop when it hits the same sum as the cash.

			CashText.Text = ""..player.Cash.Value.."" --Updates to the text to whatever the cash value is just in-case there was some issue and the label isn't displaying it right.
			ChangingValue = "Waiting"--same as before
		elseif ChangingValue == "Adding" or ChangingValue == "Subtracting" then
			print("Do nothing") --This stops spam and was an attempt at stopping the bug I'm dscriving. Failed.
		end
	end)
end)

Something that catches my eye is that the function is connecting a new function. Clearly this is not intended, is it? Function inside another function creates duplicates of the connection each time the remote is called.

Try adding print after .Changed event. I suspect the amount of prints increases after each call.

Alright, correct me if I’m wrong but I’m truly confused here. Isn’t Remote.OnServerEvent a server-only event? Shouldn’t you be using Remote.OnClientEvent and if not, then how/why is a server-script changing text inside of the PlayerGui?

Edit: I wrongly assumed PlayerGui instances were not accessable by the server when in-fact they are.

That’s right.
Also, change the wait time from

wait(.000001)

to

wait(0.1)

A misconception. OnServerEvent works with FireServer, FireServer is fired by client where OnServerEvent is the connection from the FireServer.

OnClientEvent is the other way around.


Therefore, you have to rely on PlayerGui manipulation by firing the remote to client.

Are you saying the I could do “OnServerEvent” in a localscript and vice versa?

No, you should use OnClientEvent, hence that PlayerGui should be local and player-specific only. Thus, you’ll have to use FireClient with the correct player argument from the server.

Adding a print after the .changed event does work since it printed out my “Test”

(sorry, replied to the wrong person originally) it’s that speed because I want it to visibly increase but not too slowly and .1 is not quick enough for it to catch up with the players actual currency value. I’m open to any suggestions for improvements though

If I recall this correctly, TweenService can actually shift number values. Therefore, one .Changed event and TweenService would ideally give you the same result but in a different and better method.