Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Problem with the a touch system?

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.StarterGui.ScreenGui.TextLabel.Text = "Hello"
	end
end)

I need help with this but it gives me errors in output

2 answers

✓ Accepted answer

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.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
        local playerGui = player.PlayerGui
        playerGui.ScreenGui.TextLabel.Text = "Hello"
    end
end)

---------------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".

You are searching the player's body for the starter GUI, It doesn't exist there, it exists in the player not their body, try this.

	script.Parent.Touched:Connect(function(hit)
	    if hit.Parent:FindFirstChild("Humanoid") then
	        game.Players:WaitForChild(hit.Parent.Name):WaitForChild("ScreenGui").TextLabel.Text = "Hello"
	    end
	end)

This discussion is preserved as it was. New answers, comments, and votes are closed.