Help with script getting player.Character

Hello!

I need help with a script to update a starterGui’s text based on a player’s value inside their character.

My current script is:

while true do
	wait(0.5)
	game.StarterGui.StageCounter.Frame.Counter.Text = player.Character.StageCounter.Value
end

You can’t do game.StarterGUI you must do it like this

game.Players.LocalPlayer.PlayerGUI.StageCounter.Frame.Counter.Text = player.Character.StageCounter.Value

Think it’s better to use Changed instead of a while loop, put a localscript in StarterCharacterScripts with this?

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local playerGui = player:WaitForChild("PlayerGui")

local counter = PlayerGui:WaitForChild("StageCounter").Frame.Counter

local value = character.StageCounter

counter.Text = value.Value

value.Changed:Connect(function(newVal)
	counter.Text = tostring(newval)
end)

Get the counter thing from the PlayerGui firstly and then set the text of the counter to be the current value and then when the value updates/changed, the counter changes too, the baiscs of waht is required. Note that I didn’t say to put a localscript in the counter because the character reference will change when the character dies, a localscript like this in StarterCharacterScripts ensures it’ll get the latest reference, plus the value will be destroyed anyways so you’d have to make another connection