I have this brick that destroys head on touch through a ontouch function, but i dont know how to get a player? Even if i type my own name, it only works once, then it fails,
You use the GetPlayerFromCharacter(chr) method of Players.
script.Parent.Touched:connect(function(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) print(player) end)
However, this code is bad. If an AI or another brick touches this brick, we will get an error because there is no player. We can fix this with a simple if statement.
script.Parent.Touched:connect(function(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) then local player = game.Players:GetPlayerFromCharacter(hit.Parent) print(player) end end)