If you have the character model of a player then what is the best way to get the player? I know that you can say:
game.Players:FindFirstChild(character_model.Name)
but is there another better way to do this in?
There's an actual function for this, :GetPlayerFromCharacter(character)
.
The parameter is the character instance you want to use to get to the character.
For example, a touched part:
local plrs = game.Players script.Parent.Touched:Connect(function(hit) local plr = plrs:GetPlayerFromCharacter(hit.Parent) if plr then print("Player"..plr.Name) elseif not plr then print("Player not found. Hit is something else.") end end)
If it is a tool you can get the character like this:
tool = script.Parent -- if the script is inside the tool local character = tool.Parent -- if the tool is equipped, it will be in the character local player = game.Players:GetPlayerFromCharacter(character) -- A pre-build function made by Roblox to get the player from the character (local character).
If you want to get the player from a part:
local part = script.Parent --Script's parent is a part if the script is inside that part local plr = game.Players -- Players service part.Touched:Connect(function(touch) -- function to check is something touched the part local character = touch.Parent --The object that touched the part local player = plr:GetPlayerFromCharacter(character) --your code if you need it end)
I hope that this answer has been useful and that i helped you solving your problem. For further informations, use this link: