How to find the player from humanoid?

How can I found the player from the characters humanoid?

2 Likes

Not entirely sure what you mean but you can do humanoid.Parent.Name to get players name then do game.Players:FindFirstChild(playerName) to get the game player

5 Likes

There’s a function called GetPlayersFromCharacter() and it’s under player service, so you can use that too

1 Like

Would doing Humanoid.Parent:GetPlayerFromCharacter() work?

1 Like

No, it’s game.Players:GetPlayerFromCharacter(hit.Parent) or game.Players:FindFirstChild(hit.Parent.Name)

You can use this
local players = game:GetService(‘Players’)
local player = players:GetPlayerFromCharacter(humanoid.Parent)

4 Likes

How would I do that but get it from the humanoid?

This doesn’t seem to print the name of the hit player.

Oh of the hit player ok I got you!
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(‘Humanoid’) then – makes sure it could be a player!
local char = hit.Parent
local players = game:GetService(‘Players’)
local player = players:GetPlayerFromCharacter(char)
print(player.Name)
end
end)

this should print the players name when they touch the part or whatever it is

Ok, so now I have the player who died, but how can I check that he died due to being killed by the shooting player?

Well can I see the code that you use to damage the player. Like all of it

Kon’nichiwa! :bowing_woman:t2:
When your weapon damages the player add a tag to track that the player was killed by the projectile if the player is still alive.

local Killer = 'Player or Character of the user of the weapon.'
--This detects if the humanoid is still alive, and if they are then it changes the attribute named "LastDamaged" to the name of the attacker.
if (Humanoid.Health <= 0 and Humanoid:GetAttribute("LastDamaged")) or (Humanoid.Health <= 0 )  then
Humanoid:SetAttribute("LastDamaged",Killer.Name)
end

This is an example of the code that detects when a humanoid dies.

--You can now check for the attribute attached to the humanoid to see who the last person to damage was while they were still alive.
Humanoid.Died:Connect(function()
if Humanoid:GetAttribute("LastDamaged") then
print(Humanoid.Parent.Name," was killed by",Humanoid:GetAttribute("LastDamaged"))
end
end)
--Result: 'iWantBananaNow was killed by Killer'

The humanoid is inside of your character, so we can define the character like this:

local Character = humanoid.Parent

Now that you have the character defined, you can use the built in function called GetPlayerFromCharacter which is part of game.Players/the players service

local Character = humanoid.Parent
local Player = game.Players:GetPlayerFromCharacter()

Inside the parenthesis, simply put the character, and that’s all you have to do
Final result:

local Character = humanoid.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
2 Likes