Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make one leaderboard stat display on my Gui?

Asked by 6 years ago

Yes, I know the basic of creating a gui. This is the script I have so far:

local plr = game.Players.LocalPlayer
local CashLabel = script.Parent.Parent.CashLabel
local leaderstats = game.Players.LocalPlayer.leaderstats

while wait() do
   plr.leaderstats.Cash.Value = script.Parent.Parent.CashLabel
end

 leaderstats.Cash.Changed:connect(function()
  leaderstats:SetAsync(plr.UserId, leaderstats.Cash.Value)
 end)

So basically, I wanted to make it where whatever their "leaderstats" is it will than display the value inside the textlabel of the gui.

I want to too keep running the script but I just never found out to do that, Instead of updating every 5 seconds or whatever.

0
CashLabel.Text = leaderstats.Cash.Value seith14 206 — 6y

3 answers

Log in to vote
1
Answered by 6 years ago

Hello,

If you want it to keep updating you need to put it on a loop.

Here is the fixed code:

local plr = game.Players.LocalPlayer
local CashLabel = script.Parent.Parent.CashLabel
local leaderstats = game.Players.LocalPlayer.leaderstats

while wait() do
   CashLabel.Text = leaderstats.Cash.Value
   plr.leaderstats.Cash.Value = script.Parent.Parent.CashLabel
end
while wait() do
 leaderstats.Cash.Changed:Connect(function()
  leaderstats:SetAsync(plr.UserId, leaderstats.Cash.Value)
 end)
end

~Frag

Ad
Log in to vote
0
Answered by 6 years ago

Hey, what's up?

I see problems with your code, especially with my confusion on whether or not this is a LocalScript or a ServerScript, since you're :SetAsync'ing which can only be accessed by the sever, and changing TextLabels which logically should only be done on the client. (Your DataStores are also done incorrectly)

Putting that aside, I'll just be answering your question, specifically with this block of code:

while wait() do
   plr.leaderstats.Cash.Value = script.Parent.Parent.CashLabel
end

All TextLabels have a .Text property, which displays their text. Naturally, this is how you'd manipulate it:

script.Parent.Parent.CashLabel.Text = "Test"

So with your current code, I would change it to something like this

while wait() do
   script.Parent.Parent.CashLabel.Text = plr.leaderstats.Cash.Value
end

I would not suggest looping & changing it every 1/30 of a second. It's mostly inefficient. Use the .Changed event:

plr.leaderstats.Cash.Changed:Connect(function()
    script.Parent.Parent.CashLabel.Text = plr.leaderstats.Cash.Value
end)
-- this will also update instantly

Also, don't forget to add in some variables. Less variables is more efficient, but no variables at all is completely bad practice:

local cashLabel = script.Parent.Parent.CashLabel
plr.leaderstats.Cash.Changed:Connect(function()
    cashLabel.Text = plr.leaderstats.Cash.Value
end)

Hope I've helped!

0
The parameter in changed event for ValueBase objects is always the value. To shorten your code, you could just assign Text to the value parameter. User#19524 175 — 6y
0
Okay so it work in studio but not in the real game :/ NeriodKid 6 — 6y
Log in to vote
0
Answered by 6 years ago

no every one here is wrong its simple you have a number right as your cash but TEXTlabels only take strings or text so there is a function called tostring() heres how it works

textlabel.Text = tostring(plr.leaderstats.Cash.Value)

lets say you want to get the number from the string well heres what you do

plr.leaderstats.Cash.Value =  tonumber(textlabel.Text)
0
Localscript or regular script because it's not working lol. NeriodKid 6 — 6y
0
both SHOULD work mattchew1010 396 — 6y

Answer this question