You can write your topic however you want, but you need to answer these questions:
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)
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.
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