Screen gui text label wont update after event

Hi. i’m trying to get the screen gui text label called “scoreText” to update to the string “Hello” after the player touches “Part2”. It won’t work and i’m nto sure why

local part2 = game.Workspace.Part2
local score = 0
game.StarterGui.ScreenGui.scoreText.Text = "Hi"

function onTouch(hit)
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		print("Before update:", scoreText)
		score = score + 1
		print(score)
		game.StarterGui.ScreenGui.scoreText.Text = "Hello"
		print("after update:", scoreText)
	end
end

part2.Touched:Connect(onTouch)

eveyrthing else is fine. the score variable is being updated (i put the print statements to check), the text label is being updated to “Hi” in the beginning and the function + event is fine. it’s just the text label is not being updated and i’m not sure why. can you help me fix it please

if this is being run by a server script and not a local script it will update StarterGui which doesn’t change anything on the client, also change game.StarterGui to game.Players.LocalPlayer.PlayerGui

local part2 = game.Workspace.Part2
local score = 0
game.Players.LocalPlayer.PlayerGui.ScreenGui.scoreText.Text = "Hi"

function onTouch(hit)
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		print("Before update:", scoreText)
		score = score + 1
		print(score)
		game.Players.LocalPlayer.PlayerGui.ScreenGui.scoreText.Text = "Hello"
		print("after update:", scoreText)
	end
end

part2.Touched:Connect(onTouch)

I tried replacing the game.StarterGui but it says
attempt to index nil with ‘PlayerGui’

uh if it is for everyone then you want to run

local players = game.Players:GetChildren() 
for i=1, #players do 
    local plr = players[i] 
    plr:WaitForChild('PlayerGui', 1).ScreenGui.scoreText.Text = "Hello" 
end

Maybe don’t copy this I forgot the tabs

i watched a video and moved the script to be under the text label GUI itself and then game.StarterGui.ScreenGui.scoreText.Text to script.Parent.Text

it finally worked but do you get why? is this what people usually do or is there a more efficient way

local part2 = game.Workspace.Part2
local score = 0
script.Parent.Text = "nothing"

function onTouch(hit)
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		print("Before update:", scoreText)
		score = score + 1
		print(score)
		script.Parent.Text = "Score: " .. score
		print("after update:", scoreText)
	end
end

part2.Touched:Connect(onTouch)```

the starter gui only happens when the place starts. also, you were trying to get a player gui in the server-side, which is not possible. it worked because it is getting the player gui in the client-side now (via local script), and it is also getting the player gui of the character that touched the part.

I get why yours it put inside the TextLabel mine can be put anywhere and effect all players (as long as it is a server script)

i see, is it because not all players will be touching the part which makes the gui individual to certain players now rather than the whole server/gui the whole server starts with?
what are other ways people get the player gui in the client side besides making a script under it?

thanks!

the player gui is individual to every player client-sided, there is no server-sided counterpart of it, this is why every player has their own gui.

by using a localscript, you can access the local player gui by typing

local playerGui = game.Players.LocalPlayer.PlayerGui

usually localscripts are placed in the workspace, player gui, starterplayerscripts or startercharacterscripts. so a similar approach from what you’ve coded in the first post, but making it work this time, would be changing the script to a local script, keeping it parented to the specific part, and typing this code instead:

local part2 = game.Workspace.Part2
local score = 0
local playerGui = game.Players.LocalPlayer.PlayerGui
playerGui.ScreenGui.scoreText.Text = "Hi"

function onTouch(hit)
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		print("Before update:", scoreText)
		score = score + 1
		print(score)
		playerGui.ScreenGui.scoreText.Text = "Hello"
		print("after update:", scoreText)
	end
end

part2.Touched:Connect(onTouch)