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

How to use "GetPlayerFromCharacter" properly? [Solved]

So I just started scripting one week ago and I want to start using this. What I wanted to do is make it print the person's name that is equipping a tool on the output. Here is the script:

local Players = game:GetService("Players")
 
local tool = script.Parent.Parent

local function Equipped(a)
	local player = Players:GetPlayerFromCharacter(a.Parent)
	if not player then return end
	print(player.Name)
end

tool.Equipped:Connect(Equipped)

However, when I equip the tool, it doesn't work. Can someone please help me and teach me how to fix it? Thanks!

3 comments

0

There isn't a need for this method in such a scenario. Just use the LocalPlayer.

1

Ok thanks!

0

If it's already solved, just put [Solved] in the title to let others know it's already been answered

1 answer

The first parameter passed to the tool instance's Equipped event is the Player's mouse, which is not a child of their character. So doing a.Parent won't give you their character model. (https://developer.roblox.com/en-us/api-reference/event/Tool/Equipped)

However when a tool is equipped the tool is placed in the local player's character, so you could just do script.Parent.Parent.Parent to get their character where the tool is just script.Parent.Parent, or in your case just do tool.Parent. Additionally like mentioned in the comments, you can just use LocalPlayer.

If you wanted to write code like that though you could do it somewhat like this.

local Players = game:GetService("Players")



script.Parent.Parent.Equipped:Connect(function()
	local tvA = Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)

	if tvA then
		print(tvA.Name)
	end
end)

That's how you would do it the way you're trying to.

Note: Typed this up quickly, something might be wrong.

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