Show player money on gui

I want to make a players money show to him when he opens a shop with a text label but it doesn’t change.

These are the 2 scripts I use to get the player data and show the players money:
Local script:

local gui = script.Parent.Parent
local exit = gui.Shop.Container.ExitButton
local money = gui.Shop.Container.Currency
local itemsframe = gui.Shop.Container.ScrollingFrame
local rs = game:GetService("ReplicatedStorage")
local getdatafunc = rs:WaitForChild("GetData")

local playerdata = getdatafunc:InvokeServer()

local function updatemoney()
	money.Text = playerdata.Money .."$"
end

Server script:

getdata.OnServerInvoke = function(player)
	return sessionData[player.UserId]
end

If you aren’t already, I’d recommend using an IntValue to control the player’s money. This also means you can update it server-side and it will replicate. So you could have a LocalScript parented to the TextLabel:

local label = script.Parent
local money = --path to money here

label.Text = "$"..money.Value

local function updateMoney()
    label.Text = "$"..money.Value
end

money:GetPropertyChangedSignal("Value"):Connect(updateMoney)

Ok and what should money be equal to? I’m trying to get the data table to the local script but can’t seem to get it to work so I can’t really set the money to be equal to anything

image
As seen in the image above, the value does get changed on the server side, but the text label doesn’t change at all. I’m not entirely sure how to pass the money to the client side either…

The path to money on the server side is

sessionData[player.UserId].Money

but when I invoke the server with a remote function to pass the value to the client, it doesn’t do anything to the gui.

Ok after messing around a bit I got it to work with this.

local getdatafunc = rs:WaitForChild("GetData")

local playerdata = getdatafunc:InvokeServer()

function updatemoney()
	while true do
	if gui.Shop.Container.Visible == true then
		money.Text = playerdata .."$"
	end
	wait(1)
	end
end



local function toggleshop()
	gui.Shop.Container.Visible = not gui.Shop.Container.Visible
	if gui.Shop.Container.Visible then
		playerdata = getdatafunc:InvokeServer()
	end
end

task.spawn(updatemoney)
exit.Activated:Connect(toggleshop)

It’s not that hard of a problem. It’s just that I also need the toggleshop function to run so the script only running updatemoney isn’t good for me. I countered this with task.spawn so it always constantly check how much money you have if the gui is visible.

That’s gonna eat up a ton of memory if you’re using a while true loop. From your screenshot, it looks like you have an IntValue inside of a folder named leaderstats inside of the player.

Try this, it’s much more efficient:

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

local label = script.Parent
local money = leaderstats:WaitForChild("Money")

label.Text = "$"..money.Value

local function updateMoney()
    label.Text = "$"..money.Value
end

money:GetPropertyChangedSignal("Value"):Connect(updateMoney)
1 Like

Yea, this works a lot better. Thanks man!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.