Ad
Log in to vote
1

How to make sure that when the player touches a share his name is written in a text label?

Asked by 3 months ago

My script:(I put it in a local script)

local Part = script.Parent

Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local PlayerName = hit.Parent.Name

        script.Parent.Parent.Parent.StarterGui.ScreenGui.TextLabel.Text = PlayerName
    end
end)

1 answer

Log in to vote
0
Answered by
Xapelize 1952 Moderation Voter
3 months ago
Edited 3 months ago

wrong, this is a very common mistake which a lot of people edits screengui, yeah that works before but it's moved to playergui (located inside game.Players.LocalPlayer)

you are going to do:

local Part = script.Parent

Part.Touched:Connect(function(hit)
    if hit.Parent.Name == game:GetService("Players").LocalPlayer.Name and hit.Parent:FindFirstChild("Humanoid") then
        local Character = hit.Parent -- Usually we get the character, not name
    local PlayerGui = game:GetService("Players").LocalPlayer.PlayerGui

        PlayerGui:WaitForChild("ScreenGui").TextLabel.Text = Character.Name
    end
end)
0
this might be a little confusing but in the if-then statement, i have made two statements, the first one is what i changed, it detect if the touched player name is equaled to localplayer name Xapelize 1952 — 3mo
0
the reason i located playergui is because script too long lol Xapelize 1952 — 3mo
0
thanks it works! creepycanary 12 — 3mo
Ad

Answer this question