How to show the money "cash" in the 'textlabel'?

Hello I would like to know how I can make each player see their money in their ‘gui’, how can I do that?

In short, I need to show the same cash value that is on the leaderboard and in the gui of each local player, how could i do that? could someone help me, please… :frowning:

ServerScript:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local data
	
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-cash")
	end)
	
	if success then
		cash.Value = data
	else
		print("There was an error with getting your data.")
		wait(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
	end)
	
	if success then
		print("Player data successfully saved!")
	else
		print("There was an error when saving data.")
		warn(errormessage)
	end
end)
2 Likes

In a local script that is a child of the textlabel. Sorry I had a few typos so if it doesn’t work recopy it.

local player = game.Players.LocalPlayer --define player

local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash") --define cash

cash.Changed:Connect(function() --whenever cash changes
        script.Parent.Text = "Cash: "..cash.Value --set text
end)
6 Likes

Local script in the GUI:

local textlabel = --path to the text label
local player = game:GetService("Players").LocalPlayer
local stats = player:WaitForChild("leaderstats")
local value = stats and stats:WaitForChild("Cash")
if (value) then
	value.Changed:Connect(function()
		textlabel.Text = "Coins: $"..value.Value
	end)
else
	warn("stats not loaded")
end
1 Like

Didn’t worked, it show me an error :frowning:

This local script should be a direct child of the text label. You currently have it under the screen gui.

2 Likes

You need to make sure you are actually pointing to the TextLabel.
Try changing the script.Parent.Text = ... to
script.Parent.TextLabel.Text = ...
If you have the script in the ScreenGui.

Or do what @XdJackyboiiXd21 said and move the script.

1 Like

Oh I just realized that I did not refer to the textlabel, thank you so much guys !! really :smiley: This works

1 Like