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

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.

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

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.

    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)

Answer this question