Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Problem with the a touch system?

Asked by 3 years ago
1script.Parent.Touched:Connect(function(hit)
2    if hit.Parent:FindFirstChild("Humanoid") then
3        hit.Parent.StarterGui.ScreenGui.TextLabel.Text = "Hello"
4    end
5end)

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

2 answers

Log in to vote
0
Answered by 3 years ago

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.

1script.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"
6    end
7end)

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

Ad
Log in to vote
0
Answered by 3 years ago

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.

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

Answer this question