How do i make it so that my money stat is displayed on a UI?

I’m trying to make it so that the money you have is displayed on a UI simply saying: “(xamount) ₽”
here’s what I have for the money script.

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Rubles")




local remote = game:GetService("ReplicatedStorage").Remotes.GiveMoney




game.Players.PlayerAdded:Connect(function(player)
	
	local moneyValue
	local success, err = pcall(function()
	moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	
	
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local money = Instance.new("IntValue")
	money.Name = "Rubles"
	money.Parent = leaderstats
	if success then
		money.Value = moneyValue
	else
		print("failed to load data")
	end
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
	end)
	if success then
		print("Saved Data")
	else
		print("failed to save data")
	end
end


local function autosave()
	while wait(10) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save(player)
			
		end
		
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Rubles.Value += amount
end)
spawn(autosave)

How would I make this value display on a UI?

1 Like

You have a couple of options of how to display money, but its best to not overcomplicate.
All money should be stored on the Server, so displaying it is for mere convenience.

The most conventional approach is to follow what the Server has stored for that player.
When the value is updated, it is imperative that the Player receives the change and responds to it accordingly - in this case by updating displays.

I recommend setting up each of your display labels with the Collection Service tags.
You can then use CollectionService:GetTagged(“MoneyLabels”) (Choose whatever tag name you like) to get all displays that need updating.

When the Server registers that there is a change, the Client then receives this change with a Remote Event, and updates all labels with a for loop.

Upon starting the game, all labels with this tag should get updated as well.

Since you’re using leaderstats, you can detect changes on the client without the need for remotes. Just set up a simple local script that listens for changes, and update the label accordingly.

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

local MoneyLabel = -- path to your ui

local function SetMoney()
	MoneyLabel.Text = "Current money: " .. leaderstats.Rubles.Value 
end

leaderstats.Rubles.Changed:Connect(SetMoney)
SetMoney()
1 Like

do I just put it under the ui and put the ui’s name?

Put that right in the StarterGui or StarterPlayerScripts and that is a great way to do it. Literally takes care of itself.

sorry for a very late reply, but i’m getting a red line underneath the local in local function

A remote to give money with no restrictions, sanity checks or security… what could go wrong? Don’t just rely on Byfron. I’d advise you to change that.

Money isnt exactly gonna be a key feature of my game, won’t matter too much. I have no idea how to make anything better

The path to the textLabel should exist, you didn’t have it placed yet. In case you don’t know how, add a ScreenGui under StarterGui and add a TextLabel under that ScreenGui.
Then you can refer to it like this:

local MoneyLabel = LocalPlayer.PlayerGui.ScreenGui.TextLabel

Why would you even be adding money to the player from the client? You should do this from the server if you’re able to. I don’t see why there’s a money remote if you’d be able to explain it? Not really relevant to your post just curious.

I followed a tutorial so I have no idea. I am not great at scripting so its the best i could do. Could you possibly provide a more secure option?

1 Like