Setting up a money label

Hello everyone!

Today i’m seeking out for some help. Getting kinda frustrated here with a rather simple script. Atleast simple for some of you. If i read a script, i can kinda tell what happens, but i just can’t write a script myself.

So getting to what i’ve got problems with, is simple. I’m currently working on a small sideproject and getting my hands on the tycoon world. For this i’m using the Tycoon Kit provided by Zednov. Some of you might know that.

Since i don’t like the new leaderboard (and it’s going to be a single player tycoon btw), i want to let the player see their current money provided by a label on their ui. So after many hours of googling, try and error and getting frustrated, i’m finally gotten myself to ask in this forum. What am i doing wrong?

I have created a TextLabel with a script (no localscript), that is supposed to change the text to the current money.

Script:

local player = game.Players.LocalPlayer
local cash = player.leaderstats.Cash


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

cash.Changed:connect(function()
	script.Parent.Text = cash.Value.."$"
end)

So i tried multiple ways, but it always seems to fail getting the Value. Using this script i’m getting the following error:

Players.Night2078.PlayerGui.money.Script:2: attempt to index nil with 'leaderstats'

I hope you can help me.

That is your issue. Server has no concept of “local player” and server scripts don’t run on a specific client. Use a LocalScript. That is what they are for. Handling things only one player sees.

1 Like

This is happening because you’re using a server-sided script (Script), instead of a LocalScript. Server scripts run on the server, which means there is no “LocalPlayer” property; the LocalPlayer property instead can only be accessed from a LocalScript, which runs on an individual, “local” player’s machine.

This is a really important concept for game development on Roblox, and I’d suggest that you take the time to understand how the client-network model works fully before continuing. Good resources start here: Client-Server Runtime | Documentation - Roblox Creator Hub

Okay. That’s odd. I’ve read so many different things during my search on google. Some said, it has to be a global script, some it has to be local script.

Anyway, i changed it. But now it’s getting another error.

leaderstats is not a valid member of Player

But it is, or am i dumb? xD

leaderstats

Are you running it on a LocalScript now? You can use the WaitForChild method to wait for the leaderstats object to load in, because it’s not guaranteed that things will exist as soon as a LocalScript runs. (Objects have to first be replicated, or “loaded”, from the server).

Example code:

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local cashLeaderstat = leaderstats:WaitForChild("Cash")
1 Like

Oh my god.

Thank you very much. That made it, it’s working now.

Thanks.