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

How would i get a players name that touched a part?

how would i get a players name that touched a part any articles on this?

3 answers

✓ Accepted answer

Ok. Let's make a function that will run when something touches the part. (put the script in the part you want the player to touch)

script.Parent.Touched:Connect(function(hit)
	
end)

The "hit" after the word "function" is the thing that touched the part.

That function will run everytime when something touches the part. That will include other parts such as Baseplate, etc. So let's add an if statement to know it the part that touched it is a PLAYER.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		
	end
end)

I wrote if hit.Parent and not just hit, because the player's character is a model and when a model touches a part, this function will not run. This function will only run when a PART touches the other part. So hit will maybe be the player's right leg, left leg, etc. Now that we know that the thing that touched the part is a player, let's get the player's name into a variable.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local PlayerName = hit.Parent.Name
	end
end)

hit.Parent is the player's character. The player's character will always have the same name as the player's name.

Hope this helped!

I spend 10 min writing this, so please let me know if there's a problem somewhere in the script.


local Part = workspace.Part


Part.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then
	
local PlayerName = hit.Parent.Name

end


end)


i don't know if this works or not, writing this from the top of my head

2 comments

0

You need to say game.Workspace, not just Workspace

0

No ^, workspace is a universal variable representing game.Workspace

This task is fairly simple. I recommend doing your own research and attempting your ask before asking for help. I will give you a list things to research that can be implemented into finding specifically a player's name that touched a part AND I will show you the code. It is up to you, how you will use what I give you. Although, I don't recommend to copy and paste. Please learn to program in LUA, and use what I have given you, to make your own script, and use my script to reflect on.

Research List: Variables, Conditional Structures, Players:GetPlayerFromCharacter, BasePart.Touched

-- Defining Global Variable "part"
part = workspace.Part

-- Touched Event
part.Touched:Connect(function(hit)

	-- Conditional Structure (If Statement)
	if hit.Parent:FindFirstChild("Humanoid") then
		-- if thing that touched the part has a humanoid in the parent then
		
		-- Defining Local Variable "character"
		local character = hit.Parent
		
		-- Conditional Structure (If Statement)
		if game:GetService("Players"):GetPlayerFromCharacter(character) then
			-- if the character that touched the part is a player then

			-- Defining Local Variable "Name"
			local Name = character.Name
			-- character name is written the same as player name
			
			print(Name)
		end
		
	end

end)

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