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)
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.
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!