Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to find player's username upon Touched event?

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.

4 comments

0

if the part is touched, should the player that touched it gets the changed text?

0

You're not going to be able to manipulate player's PlayerGui in a script

0

this might work mabye idk

0

@Vulkarin the script works

2 answers

✓ Accepted answer

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

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.