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

How do i get a player?

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,

1 answer

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)

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