--!strict


local Players = game:GetService("Players")

local weaponUtil = {}

function weaponUtil.getHumanoid(part: Part)
	while part and part ~= workspace do
		if part:IsA("Model") and part.PrimaryPart and part.PrimaryPart.Name == "HumanoidRootPart" then
			return part:FindFirstChildOfClass("Humanoid")
		end

		part = part.Parent
	end
end

function weaponUtil.getPlayerFromHumanoid(humanoid: Humanoid)
	for _, player in ipairs(Players:GetPlayers()) do
		if player.Character and humanoid:IsDescendantOf(player.Character) then
			return player
		end
	end
	
	return nil
end

function weaponUtil.getAnimatorFromPlayer(player: Player)
	assert(player.Character, string.format("Unable to get animator for player %s. Player does not  have a character.", player.Name))

	local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
	local animator = humanoid:FindFirstChildOfClass("Animator")

	return animator
end

return weaponUtil
