Showing Cash in TextLabel

Could you please elaborate on this?

Apparently this is correct, because the path to the leaderboard is correct. I found it strange why it didn’t work, everything is correct and the code makes absolute sense. I tried changing the path to game.Players.leaderstats but that didn’t work either. I think the problem is when the variable tries to locate the “leaderstats”, because it is inside the player. Maybe “:GetDescendants” can help, but I’m not sure.

This kind of reference is getting wrong because you’re assigning it to the player service, it isn’t tagged to the actual player, i recommend storing service as variable first then get the actual player.

it’s supposed to look like this

--assuming this is in ScreenGUI as LocalScript
local playerSV = game:GetService("Players")
local player_crv = playerSV.LocalPlayer
local cash_val = player_crv.leaderstats:WaitForChild("Cash")

cash_val.Changed:Connect(function()
      script.Parent.Text = cash_val.Value
end
2 Likes

Here, this should solve the issue for you.

--[[Put this inside of a local script, and put that local script inside of the TextLabel]]
local LeaderStats
for i,v in pairs(game.Players.LocalPlayer:GetDescendants()) do
	if v.Name == "leaderstats" then
		LeaderStats = v
	end
end

if LeaderStats ~= nil then
	script.Parent.Text = LeaderStats.Cash.Value
	LeaderStats.Cash:GetPropertyChangedSignal("Value"):Connect(function()
		script.Parent.Text = LeaderStats.Cash.Value
	end)
end
2 Likes

Worked perfectly! Thank you very much for helping me and expanding my knowledge :wink:

1 Like

Sorry work ran late had to get home so I could test this for you …
This is the whole thing from a server script.

-- in ServerScriptService
local Players = game:GetService("Players")

local function Setup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 0
	cash.Parent = leaderstats
end

Players.PlayerAdded:Connect(Setup)

while true do task.wait(5) -- this would be 60
	for _, player in pairs(Players:GetPlayers()) do
		local playerGui = player:WaitForChild("PlayerGui")
		local moneyLabel = playerGui:WaitForChild("ScreenGui"):WaitForChild("Frame"):WaitForChild("TextLabel")
		-- line above has to be set to the textlabel

		local leaderstats = player:FindFirstChild("leaderstats")
		local cash = leaderstats:FindFirstChild("Cash")

		cash.Value +=20
		moneyLabel.Text = tostring(cash.Value)
	end
end
2 Likes

nope not at all, ok so what you are gonna want to do in a script (PS put the script in ServerScriptService) put this code into said script:

	game.Players.PlayerAdded:connect(function(plr)
		local stats = Instance.new("BoolValue",plr)
		stats.Name = "leaderstats"

		local cash = Instance.new("IntValue",stats)
		cash.Name = "Cash"
		TextLabel.Text = tostring(cash.Value)
	end)

what this code will do is create the leaderstats inside of the player and put the cash value into the leaderstats. Now the reason we do this via the server AKA with a script is cause if we do it via a local script no one else will be able to see the value and if its on the client it will be very easy for exploiters to mess with.


Next to display the cash value on the player we will have to do the following,
Create a local script and put it inside of the text label, put this code in it

local player = game.Players.LocalPlayer -- local player is a way that we can get the player that is using this GUI, note that the LocalPlayer value is only accessible via the client, for more info read up on the player document, https://create.roblox.com/docs/reference/engine/classes/Players
local leaderstats = player:WaitForChild("leaderstats",30) -- the reason we wait for it is cause when we load in it could not be there yet so we must wait for it to be there.
local cash = leaderstats:WaitForChild("Cash",30) -- get the cash value

local textLabel = script.Parent

cash:GetPropertyChangedSignal("Value"):Connect(function()
      textLabel.Text = cash.Value
end)

now in theory this should work, if there are any error please tell me! Hope this works!
Nvm see someone else already solved it!

1 Like

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