Money GUI not updating ~ Need help!

Hi there! I have been trying to make a money system. It’s kind of difficult for me since I am a rookie on scripting. I am not sure if I did my script correctly. I have an issue because the Money GUI is not updating! Even when I re-join! Here is the video:

This is the code that I probably did wrong:

local DataStoreService = game:GetService("DataStoreService")
local Money = DataStoreService:GetDataStore("Money")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Money"
	Cash.Parent = leaderstats
	
	local Id = "Player_"..player.UserId
	
	local data
	local success, errormessage = pcall(function()
		data = Money:GetAsync(Id)		
	end)
	
	if success then
		Cash.Value = data
	end
	
	game.StarterGui.CURRENCY.CASH.VALUE.Text = (Cash.Value.."$")
end)

Players.PlayerRemoving:Connect(function(player)
	local Id = "Player_"..player.UserId

	local success, errormessage = pcall(function()
		Money:SetAsync(Id, player.leaderstats.Money.Value)
	end)

	if success then
		print("Data successfully saved!")
	else
		print("Something went wrong while saving data...")
		warn(errormessage)
	end
end)

Side note: Optional help but is there a way so if the value is bigger then 1,000 it will turn to 1K or like 1,000,000 to 1M please? Thanks!

Akridiki

6 Likes

The problem is you are changing the StarterGui when you should be changing the player’s PlayerGui instead.

Additionally, you should create a .Changed connection on the client for the IntValue so you can update the GUI each time the player’s money is changed.

2 Likes

So uhm about that. Do I have to like, clone the gui into the player’s GUI? Then do the .Changed connection?

Roblox automatically clones the GUI into the player’s PlayerGui

Instead of game.StarterGui.CURRENCY.CASH.VALUE.Text = (Cash.Value.."$")

You can write player.PlayerGui.CURRENCY.CASH.VALUE.Text = (Cash.Value.."$")

2 Likes

I suggest you use something like this in a local script inside the text gui you want to change:

local player = game.Players.LocalPlayer
local StatFolder = player:WaitForChild("leaderstats") -- Change to your stat name
local Stat = StatFolder:WaitForChild("Money") -- Change to your currency name 

script.Parent.Text = Stat.Value.."$"

Stat.Changed:Connect(function()
    script.Parent.Text = Stat.Value.."$"
end)

You should only change the player gui using local scripts running on the client. Place the code I gave you above in a local script in the text element you want to change. Hope this helps.

If this work please mark it as solution.

2 Likes
local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")

local Money = leaderstats:WaitForChild("Money")

while true do
   wait()
    script.Parent.Text = tostring(Money.Value)  -- Converting IntValue to string
end
1 Like

I’m pretty sure this wouldn’t work, because your setting a string to a number value consider using “tostring”

1 Like

I strongly suggest against using while loops as they can cause a lot of lag use to Changed or GetPropertyChangedSignal() Instead to help minimize lag.

1 Like

It would trust me, I use this in every game and it always work and tosting() is for string values not numbers, numbers can be directly displayed.

2 Likes

This is the error, you’re using the StarterGui and don’t listening if the value changes
obj

You can use the following script (replace the line up with this code)

Cash.Changed:Connect(function()
   player.PlayerGui.Currency.Cash.Value =  "$"..tostring(Cash.Value)
end)
1 Like

Please read the above post which have all said what you just put into one post before replying.

1 Like

I read it, just resumed things.

1 Like

It looks like you have made your mistake here.

game.StarterGui.CURRENCY.CASH.VALUE.Text = (Cash.Value.."$")

All this is doing is changing the text label in the StarterGui. When a player loads into the game, Roblox takes a copy of the GUIs from StarterGui and puts them into PlayerGui. Each player has their own GUI. You could find it by doing Player:WaitForChild("PlayerGui").

To correct your mistake; you will want to add a LocalScript, a script that will only affect what the player sees, and put it in the TextLabel that displays the money.

local Player = game:GetService("Players").LocalPlayer
local Money = Player:WaitForChild("leaderstats"):WaitForChild("Money")
local Gui = script.Parent

Gui.Text = tostring(Money.Value)


Money:GetPropertyChangedSignal("Value"):Connect(function()
	Gui.Text = tostring(Money.Value)
end)

Hope this helps!

2 Likes

Are you checking if the value has changed to update the GUI when it changes? If you didn’t, thats why, if you did, I’m blind.

About the side note, I don’t know how to right now, but I did something like that once.
People are saying the same thing, it’s kind of obvious, but don’t worry, there are always dumb fails.

1 Like
local money = player:WaitForChild("leaderstats"):WaitForChild("Money")

local text = script.Parent
local function update()
	text.Text = "$" .. money.Value
end

update()
money.Changed:Connect(update)

I have fixed it everyone! Thanks @EmbatTheHybrid for the help and the rest of you! ^

3 Likes