Displaying a score on a ScreenGUI

I’m pretty new to Roblox Studios scripts so I started out by doing a simulator tutorial so I can learn how scripts function. I had already had the selling, earning points to sell, and leaderboards stuff setup when I tried tackling displaying the players score “Sugar” on a ScreenGUI. But when I tried coding the script to display the players “Sugar”, I was hit with a error that said that [Players.LightGuardian9.PlayerGui.Main.Sugar.ShowSugar:3: attempt to index nil with ‘WaitForChild’]. Am I doing something wrong in my script? I tried rewriting it multiple times but still got nothing.

-- 
while wait() do
	local player = game.Players.LocalPlayer
	script.Parent.Text = "🍭"..player:WaitForChild("leaderstats"):FindFirstChild("Sugar").Value
end
2 Likes

Maybe define the player outside the while loop? Also, try to define player as game:GetService("Players").LocalPlayer.

Otherwise, I see no reason why this should fail.

2 Likes

Is your script a Script or a LocalScript?

You might try this (If it doesn’t work, please, be aware that I didn’t tested the code):

local player = game.Players.LocalPlayer
local Sugar = player:WaitForChild("leaderstats").Sugar

script.Parent.Text = "🍭 "..Sugar.Value

Sugar:GetPropertyChangedSignal("Value"):Connect(function()

    script.Parent.Text = "🍭 "..Sugar.Value

end)
1 Like

Thank you for the help. It turns out that I was using a script instead of a local script.

1 Like