Hi all! Today I will be showing you how to load a player’s leaderstats in a GUI!
(Note: Please ignore the other things you might see in the explorer as am working on a game but just came up with the idea to make this tutorial to help others)
Add a script inside ServerScriptService and write in it this code:
game.Players.PlayerAdded:Connect(function(player)
print("player found")
local leaderstats = player:WaitForChild("leaderstats")
print("leaderstats found")
local cash = leaderstats:WaitForChild("Cash")
print("cash found")
local cashscreengui = player.PlayerGui:WaitForChild("Cash")
local cashgui = cashscreengui:WaitForChild("TextLabel")
print("Cash GUI found")
cashgui.Text = cash.Value
cash.Changed:Connect(function()
cashgui.Text = cash.Value
end)
end)
Code explanation
The 1st line gets the player.
Lines 2 - 7 get the stats etc. (all the prints are to verify the script is working at each step)
The line cashgui.Text = cash.Value sets the value
Lines 8 - 10 “update” the GUI if the player’s leaderstats change.
And done!
Extra Details
Make sure your explorer matches to the screenshot in Step 1 (please ignore the other 2 GUIS)
Make sure all scripts are enabled.
Make sure to change the names to your GUIs if your having different names.
If it’s still not working please contact me on the devforum or on discord anirudh851#3224. Messaging on the devforum is recommended as I don’t see discord quite often.
If you can’t make it then here is the CashScript: CashScript.rbxm (812 Bytes)
Put the script in ServerScriptService
Here is the GUI: CashGUI.rbxm (2.5 KB)
Put the GUI in StarterGui
Thanks for reading and hope this helps! Feel free to give feedback below!
I might be missing something, but how does this line work?
Don’t you need to change the PlayerGui, not the StarterGui? Also the player added function will mean that everyone’s gui will change whenever someone else’s cash changes.
Please change this to say “Add a LocalScript”
You also don’t need the onplayeradded in a local script. Just do something like:
local player = game:GetService("Players").LocalPlayer
local leaderstats = player:WaitForChild("leaderstats",20)
local cash = leaderstats:WaitForChild("Cash",20)
local cashGui = script.Parent
cashGui.Text = cash.Value
cash.Changed:Connect(function()
cashGui.Text = cash.Value
end)
Hi again! Instead of changing it to a localscript I kept it in a script and it works now. Tested with my both accounts together and it showed different for both of them.