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"
local leaderstats = player:WaitForChild("leaderstats")
local eggs = leaderstats:WaitForChild("Eggs")
local gold = leaderstats:WaitForChild("Gold")
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"
local leaderstats = player:WaitForChild("leaderstats")
local eggs = leaderstats:WaitForChild("Eggs")
local gold = leaderstats:WaitForChild("Gold")
local UpdateFrequency = 5
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:
While loops
##Accept and upvote if this helps!
2 comments
Did you even try
Also I edited the indentation of your end).