Fixing function to find humanoid, with type-checking

What do you want to achieve?

I have created a function that finds and returns the Humanoid of a Player’s Character, for the purpose of custom admin commands in a ModuleScript. However, there seems to be a problem.

What is the issue?

Apparently, the 6th, 7th, and last lines of the function are considered incorrect.

--!strict
function findHumanoid(Player:Player):Humanoid
	local Character:Model? = Player.Character
	if Character then
		local Soul:Humanoid? = Character:FindFirstChildWhichIsA("Humanoid")
	end
	if Soul then
		return Soul
	end
end
  • if Soul then - unknown global ‘Soul’
  • return Soul - unknown global ‘Soul’
  • end - Not all codepaths in this function return ‘Humanoid’

What solutions have you tried so far?

I reread intelsyntax_enjoyer’s type-checking tutorial if I did something wrong. I even had to explicitly write the types of variables and what the function should return; it does not work. I also did if-else statements.

What am I missing?

EDIT: I rewrote the function, but at the last line, the end is invalid because “Not all codepaths in this function return ‘Humanoid’.”

function findHumanoid(Player:Player):Humanoid
	local Character:Model? = Player.Character
	local Soul:Humanoid?
	if Character then
		Soul = Character:FindFirstChildWhichIsA("Humanoid")
	end
	if Soul then
		return Soul
	end
end

On my end, the code works perfectly fine including the type checking. You may have something wrong going on with the whole module script. Try to check if you correctly added end.

function findHumanoid(Player:Player):Humanoid
	local Character:Model? = Player.Character
	local Soul:Humanoid?

	if Character then
		Soul = Character:FindFirstChildWhichIsA("Humanoid")
	end
	if Soul then
		return Soul
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		print(findHumanoid(plr))
	end)
end)

Here is the function’s parent ModuleScript:
AdminCommands.lua (1.1 KB)
Usually whatever is above the error area may be causing the error, and not the bottom part, I think…

function findHumanoid(Player:Player):Humanoid
	local Character:Model? = Player.Character
	if Character then
		local Soul:Humanoid? = Character:FindFirstChildOfClass("Humanoid")
		if Soul then
			return Soul
		end
	end
end

Doesn’t work; last end still displays as invalid.

If Soul is nil or false, then in Luau’s eyes the function will never return the correct type. You should change the return type to Humanoid?.

Also, in the original code, Soul is defined outside of the function’s main scope, so it doesn’t exist for the function, so if you try to use it anywhere else Luau throws a warning saying it hasn’t been defined.

--Won't work
if condition then
    local Soul:Humanoid? = Character:FindFirstChildWhichIsA("Humanoid")
end
print(Soul)
--"Soul" is defined as a variable inside of the if statement, so it doesn't exist in
--anything outside of that if statement

--Will work
local Soul
if condition then
    Soul = Character:FindFirstChildWhichIsA("Humanoid")
end
print(Soul)
--Soul is defined outside of the if statement, in the same scope as the print, and is
--assigned to inside of the if statement. By default, Soul will be nil as that it the default
--for un-initialized variables
1 Like

It worked!

function findHumanoid(Player:Player):Humanoid?
	local Character:Model? = Player.Character
	local Soul

	if Character and Character:IsA("Model") then
		Soul = Character:FindFirstChildWhichIsA("Humanoid")
	end	
	return Soul
end

Thank you!