How to print a player's username when touching an object

Hi there,

I am trying to make a script to print to the output whenever somebody touches one of the KillBricks in my game (I am making an obstacle course with laser parts that kill you) with their username. I want to achieve this as it might be useful information later in my development of the game.

Here is the script I am trying to use:

function onTouched(Obj)
	local Character = Obj.Parent:FindFirstChild("Humanoid")
	if Character then
		Character.Health = 0
	end
end
	script.Parent.onTouched(function(player)
		local username = player.Name
		print(username.Name)
	end)


script.Parent.Touched:Connect(onTouched)

Whenever I touch one of the lasers however, nothing goes into the output!

Many thanks for your help,
Mic

1 Like

Simple as using GetPlayerFromCharacter

script.Paremt.Touched:Connect(function(part)
    local player = game.Players:GetPlayerFromCharacter(part.Parent) -- Gets Player by Character
    if player then -- if there is a Player (function may return nil)
        print(player.Name)
    end
end)

1 Like

Cheers mate,

It works but you did make a spelling mistake and put script.Paremt instead of script.Parent

Once I spotted it and changed it, it worked brilliantly!

Thanks again, I’ll mark your response as the solution now.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.