Connect TextLabel to Leaderstats

I’m attempting to connect a TextLabel to my Leaderstats. Might sound confusing, so here’s an example: I have leaderstats and there’sa value of Coins. I’m trying to set the value of coins into my TextLabel.

The issue is that

  • My knowledge with Lua scripting isn’t that great
  • It’s not changing the TextLabel value.

I’ve tried scripting this:

local value = script.Parent

while true do
    value.Text = player.leaderstats.Coins.Value
end

but it returns a

I’ve looked for answers on scripting sites and the Roblox Developer Hub, either they were outdated or it was not what I was looking for.

Any help is gladly appreciated.

1 Like

What player are you trying to reference here? player is never set so it’s nil.

If it was set, this would result in an infinite loop with no yielding which will crash the game.

If you can provide more information about where this TextLabel is, and which players’ leaderstats you want it to show, then we might be able to point you in the right direction.

Use Instance.Changed, it will cause less lag

A while true do loop is rarely ever the solution, especially without a yield.

This should work:

local myValue = game.Players.LocalPlayer:WaitForChild('leaderstats'):WaitForChild('Coins') -- Waits for the leaderstats folder to be created, then waits for the 'Coins' value.
local value = script.Parent
myValue:GetPropertyChangedSignal('Value'):Connect(function() -- Listen for the 'value' property of the myValue object to change
    value.Text = myValue.Value
end
5 Likes

I agree with @Snak3GT and please also use @7z99’s script. By the way when you are using that loop you should have a wait() .

1 Like

Thank you very much! I’ve already tested it and glad to say that it works! Thank you once again!

1 Like