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

How to use GetPlayerFromCharacter Function?

Hello

I don't know why The GetPlayerFromCharacter useful.

I posted this question cause i want to know how to use the GetPlayerFromCharacter function.

I hope i get a answer!

1 answer

✓ Accepted answer

Most people use Players:GetPlayerFromCharacter() as an alternative to FindFirstChild("Humanoid"). While that is partially true, it isn't the entire story. The GetPlayerFromCharacter function returns a player instance (or nil if there isn't any.) There's a bunch of uses for GetPlayerFromCharacter, but it's mainly used in .Touched events. Look at this script:

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent) --The parameter is the character.
	if player then
		hit.Parent.Humanoid.Sit = true --It makes the player sit (or trip).
	end
end)

It only gets the players ONLY, and not the NPCs. I would imagine that you might have some sort of NPCs in your game, and you want to get the players only. Here's another script:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Sit = true --It makes the player sit (or trip), but also NPCs. It could break your script and if you wanna open a GUI, for example, as GUIs are player only.
	end
end)

so TL;DR, GetPlayerFromCharacter allows you to get the character, and seperate the NPCs, and it's basically a sort of alternative to FindFirstChild("Humanoid").

1 comment

0

Thank you!

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