Updating textlable.text with the leaderboards stats but not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to update the text with the leaderboard stat but it shows an error. I’ve tried all the ways I know but I still can’t fix it.

Here’s the scripts

--tool script


local players = game:GetService("Players")

local tool = script.Parent
local screenGUI = game.StarterGui.main.ScreenGui
local statsframe = screenGUI.stats_frame

local backpack = tool.Parent
local player = backpack.Parent
local leaderstats = player:FindFirstChild("leaderstats")
local stat = leaderstats:FindFirstChild("Cookies")

tool.Activated:Connect(function()
	stat.Value = stat.Value + 1
	print(stat.Value)
end)
--leaderboard script

local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local stat = Instance.new("IntValue")
	stat.Name = "Cookies"
	stat.Parent = leaderstats
end

Players.PlayerAdded:Connect(leaderboardSetup)
--textLable script

local player = game:GetService('Players')
local cookieLable = script.Parent
local textLabel = script.Parent
local leaderboardStat = player:FindFirstChild("leaderstats"):FindFirstChild("Points")

leaderboardStat.Changed:Connect(function()
	textLabel.Text = "Points: " .. leaderboardStat.Value
end)

this is the error message that I’m getting

As you can see every other parts of the script is running perfectly except for the text lable script. I’m still very new to developing so sorry for the stupidity.

2 Likes
textLable LN 6: Attempt to index nil with 'FindFirstChild'

This means that this line:

local leaderboardStat = player:FindFirstChild("leaderstats"):FindFirstChild("Points")

Is trying to use Instance:FindFirstChild() on something that doesn’t exist, causing an error. The reason why is because the player variable is defined as:

local player = game:GetService('Players') -- LN 3

The script tries to find a 'leaderstats' object in the Players service, and doesn’t find anything, so it returns nothing. And since you’re using Instance:FindFirstChild() on nothing, it’s causing the error.

Instead, you should use:

local player = game:GetService('Players').LocalPlayer

I tried it but it’s still making an error message.

What error are you receiving?

Character limit

Players.moganothere.PlayerGui.main.ScreenGui.stats_frame.TextLabel.Script:6: attempt to index nil with ‘FindFirstChild’

the same error

local leaderboardStat = player:FindFirstChild("leaderstats"):FindFirstChild("Points")

This line contains two Instance:FindFirstChild() functions, so it’s difficult to tell which is causing an error.

Although this is not the solution, split that line into two lines.

local leaderstats = player:FindFirstChild("leaderstats")
local leaderboardStat = :FindFirstChild("Points")

local leaderstats = player:FindFirstChild(“leaderstats”)
It’s causing the error Can you explain why it is causing the error and how I can fix it?

Is the textLable script a LocalScript or a ServerScript? I assumed it was a LocalScript, so it would use Players.LocalPlayer.

1 Like

It’s a server script. So do I need to change it to local script?

Most likely.

Character Limit

I’m seeing something called the “Character limit” in your replies

It’s working thanks for the help and sorry for the inconvinience

No worries.

The Devforums can’t have a post less than the required limit, so I had to get to that limit by adding a Character limit.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.