Scripting Helpers is winding down operations and is now read-only. More info→
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 2 years 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 2658 Moderation Voter Community Moderator
2 years ago
Edited 2 years 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 2658 — 2y
0
the reason i located playergui is because script too long lol Xapelize 2658 — 2y
0
thanks it works! creepycanary 12 — 2y
Ad

Answer this question