Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I'm trying to make a textlabel change with leaderboardstats?

This is for my simulator, I want the textlabel number change with the leaderstat eggs and another for gold.

Ex; Player receives 500 eggs and 10 gold the textlabel should then say 500 and 10 how would I do that?

****Leaderboardstat script

game.Players.PlayerAdded:connect(function(plr)
	local stats = Instance.new('IntValue', plr)
	stats.Name = 'leaderstats'
	local points = Instance.new('IntValue', stats)
	points.Name = 'Eggs'
	points.Value = 0
    
    local points = Instance.new('IntValue', stats)
	points.Name = 'Gold'
	points.Value = 0
end)

2 comments

0

Did you even try

0

Also I edited the indentation of your end).

1 answer

To achieve this, we'll have to use a LocalScript. You already have your leaderboard set up, which is great. The only issues are that the second argument of .new() is deprecated, as well as you used the same variable twice. Read here for more information on that. In the meantime, we'll just set the Parent property of each value last. Your leaderboard should look like:

game.Players.PlayerAdded:connect(function(plr)
    local stats = Instance.new('IntValue')
    stats.Name = 'leaderstats'
    stats.Parent = plr
    local eggs = Instance.new('IntValue')
    eggs.Name = 'Eggs'
    eggs.Value = 0
    eggs.Parent = stats

    local gold = Instance.new('IntValue')
    gold.Name = 'Gold'
    gold.Value = 0
    gold.Parent = stats
end)

Now that we've optimized your main script, we can focus on the client side. For this local script, we can take two different approaches, depending on how often your leaderstats are being changed. If the changes don't occur extremely often (like multiple times per second), we can utilize the .Changed event (or :GetPropertyChangedSignal(), either works). Otherwise, we can use an infinite while loop which, although I wouldn't recommend, would work just as well. Let's look at each solution individually.


###.Changed

Let's assume we have a ScreenGui in the StarterGui with a TextLabel inside it. This LocalScript will be inside the label:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local label = script.Parent
label.Text = "0 Eggs, 0 Gold" --set default
---
local leaderstats = player:WaitForChild("leaderstats")
local eggs = leaderstats:WaitForChild("Eggs")
local gold = leaderstats:WaitForChild("Gold") --wait for stats to load

eggs.Changed:Connect(function(newvalue)
   label.Text = tostring(newvalue) .. " Eggs, " .. tostring(gold.Value) .. " Gold"
end)

gold.Changed:Connect(function(newvalue)
   label.Text = tostring(eggs.Value) .. " Eggs, " .. tostring(newvalue) .. " Gold"
end)

This is great. Now everytime either the gold or eggs stats change, the label this LocalScript is inside will update. Note that, when connected to a function on an actual valueObject (i.e. an IntValue, NumValue, ObjectValue), the parameter of .Changed will be the new value of the object, rather than the name of the property changed.


Let's look at the other way with an endless while loop. I don't recommend this, but it's still doable. The same setup is expected here:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local label = script.Parent
label.Text = "0 Eggs, 0 Gold" --set default
---
local leaderstats = player:WaitForChild("leaderstats")
local eggs = leaderstats:WaitForChild("Eggs")
local gold = leaderstats:WaitForChild("Gold") --wait for stats to load
local UpdateFrequency = 5 --the rate at which stats will be updated

while true do
   wait(UpdateFrequency)
   label.Text = tostring(eggs.Value) .. " Eggs, " .. tostring(gold.Value) .. " Gold"
end

Although the code here seems simpler, I certainly would suggest using the .Changed method instead. Then, you won't be running a loop needlessly throughout your game's entire runtime. If you have any questions, feel free to leave them in the comments!


##Resources:

.Changed

While loops


##Accept and upvote if this helps!

3 comments

0

Just use string patterns instead of all that concatenation. It's a huge optimisation and it's less messy. "%d Eggs, %d Gold" where %d is a digit, and you'd format it with string.format("%d Eggs, %d Gold", eggs.Value, gold.Value)

0

Also the font is a little too big - consider making it smaller.

0

i couldnt figure out how to. whenever i make lines with --- it hecks up the formatting. but good idea on the concatenation

This discussion is preserved as it was. New answers, comments, and votes are closed.