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

Get A Single Player's Name?

The following script gets all the player's names once the script's parent (Part) is touched.

local debounce = false

script.Parent.Touched:connect(function(hit)
	
	if not debounce then
		
		debounce = true
		
		local list = {}
		
		for i, plr in pairs(game.Players:GetPlayers()) do
			
			table.insert(list, plr.Name)
			
			msg = Instance.new("Message")
			msg.Parent = game.Workspace
			msg.Text = table.concat(list, "")
			
		end
		
		debounce = false
		
	end
	
end)

What would I need to do to this script to only get 1 player's name instead of them all?

2 comments

0

Don't search through the players, check if hit.Parent is a humanoid, then use the :GetPlayerFromCharacter() method.

0

This script is going to be for a leaderboard that is displayed in a GUI at the end of the game. I only used the onTouched() function to test it. I know what you're saying, though.

1 answer

You don't need to create a table, GetPlayers already return a table. Also, that debounce is useless, because it has no wait.

local debounce = false

script.Parent.Touched:connect(function(hit)

    if not debounce then

        debounce = true

       local list = game.Players:GetPlayers())

       msg = Instance.new("Message")
       msg.Parent = game.Workspace
       msg.Text = table.concat(list, "")

        wait(1) -- the debounce wait

        debounce = false

    end

end)

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