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

How to find player's username upon Touched event?

Asked by 5 years 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 77 — 5y
0
You're not going to be able to manipulate player's PlayerGui in a script Vulkarin 581 — 5y
0
this might work mabye idk Donut792 216 — 5y
0
@Vulkarin the script works DeceptiveCaster 3761 — 5y

2 answers

Log in to vote
2
Answered by
RAYAN1565 691 Moderation Voter
5 years 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 5 years 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