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

Need Help Printing Player Name After Collision?

I need to print out the player's name in the console when they touch a brick with their character, but my code doesn't work. can anyone help me solve this issue?

--variables

local brick = script.Parent

--functions

local function startLevel(player)
	
	print(player.Name)
	
end

--code

brick.Touched:Connect(startLevel(otherPart))

1 answer

Here's an easier way:

local brick = script.Parent

function startLevel(hit)
	if hit.Parent then
		if hit.Parent:FindFirstChild("Humanoid") then
			print(hit.Parent.Name)
		end	
	end
end

brick.Touched:Connect(startLevel)

Here's how this works: Once the function is fired (when the brick is touched) it checks does the object that hit the brick still exist, if it does, then it checks does the object's Parent (Obviously the character) have a Child called "Humanoid", if it does, we can be sure the toucher was a player, and then print the player's/character's Name.

You did the variable defining correctly, but use this function instead.

3 comments

0

What if "hit" gets destroyed? What if hit isn't a player object? This would print on everything that happens to touch the object

0

Stop. Spoonfeeding. Period.

0

Looks like I did it too quickly. Fixed anyways, sorry!

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