hit.Parent is the player's character in this case.
StarterGui is not a child of the player's character.
To solve this, you have to define the player's playergui.
1 | script.Parent.Touched:Connect( function (hit) |
2 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
3 | local player = game:GetService( "Players" ):GetPlayerFromCharacter(hit.Parent) |
4 | local playerGui = player.PlayerGui |
5 | playerGui.ScreenGui.TextLabel.Text = "Hello" |
---------------EXPLANATION------------------
I'm gonna explain the code I just added in.
First, I defined the Players service, why did I do game:GetService("Players")
while I could've done game.Players
?
Well, if for some reason you were to change the name of the service, :GetService
will still return the Players service no matter what the service is called.
Then, I used a function :GetPlayerFromCharacter with the parameter being the player's character. hit.Parent as I said, is the player's character in this case.
After that, I defined PlayerGui. PlayerGui is a child of the player.
Finally, I changed the text of TextLabel to "Hello".