Ad
Log in to vote
0

How to find player's username upon Touched event?

Asked by 2 months ago

Please note that this is a rather long question and contains multiple scripts.

The following scripts associate a WORKING timer for my Ninja Warrior tournament:

Script 1 (The Timer):

local bool = true
script.Parent.Touched:Connect(function()
    if bool == false then return end
    bool = false
    if script.Parent:FindFirstChild("Humanoid") ~= nil then
        for i = 100,0,-1 do
            wait(1)
            for _,p in pairs(game.Players:GetPlayers()) do
                p.PlayerGui.ScreenGui.Timer.Text = i..""
            end
            if i == 0 then
                for _,v in pairs(game.Players:GetPlayers()) do
                    p.PlayerGui.ScreenGui.Timer.Text = "Failure!"
                end
            end
        end
    end
    wait(3)
    bool = true
end)

Script 2 (Clear Button):

local bool = true
script.Parent.Touched:Connect(function()
    if bool == false then return end
    bool = false
    if game.Workspace["Stage 1 Start Button"].Script.Disabled ~= true then
        game.Workspace["Stage 1 Start Button"].Script.Disabled = true
    end
    for _,p in pairs(game.Players:GetPlayers()) do
        p.PlayerGui.ScreenGui.Timer.Text = "Clear!"
    end
end)

Before line 8 in Script 2, I want to know how to find a player's username and show it with lines 8 and 9. Any help would be appreciated.

0
if the part is touched, should the player that touched it gets the changed text? ieatandisbaconhair 68 — 2mo
0
You're not going to be able to manipulate player's PlayerGui in a script Vulkarin 548 — 2mo
0
this might work mabye idk Donut792 75 — 2mo
0
@Vulkarin the script works MCAndRobloxUnited 587 — 1mo

2 answers

Log in to vote
2
Answered by
RAYAN1565 665 Moderation Voter
2 months ago

How to find the player's username using the Touched event?

Try to incorporate the below code into your script(s):

script.Parent.Touched:Connect(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local username = hit.Parent.Name --username would be found right here
    end
end
Ad
Log in to vote
0
Answered by 2 months ago

Hey I'm BlackOrange and I will be attempting to help.

RAYAN1565 answer is correct to some degree, but to 100% guarantee that what touched the part is INDEED a PLAYER, then you would use :GetPlayerFromCharacter. This is basically a better improved way since the script above provided would show a NPC's name if the npc happened to slight touch the part.

script.Parent.Touched:Connect(function(h)
    if h.Parent:FindFirstChild('Humanoid') and game:GetService('Players'):FindFirstChild(h.Parent.Name) then
        local plr = game.Players:GetPlayerFromCharacter(h.Parent)
        local Name = plr.Name
    end
end)

Hopefully this will help improve your script.

Best of luck developer!

Answer this question