Help in type checking

local Players = game:GetService("Players")

export type Character = Model & {["Humanoid"]: Humanoid, ["HumanoidRootPart"]: Part}

local MoveUtilities : {
	["GetClosestFromArray"]: (Character, number, {[number]: Character}) -> Character?,
	["GetAllPlayersCharacters"]: () -> {[number]: Character}
} = {["GetClosestFromArray"] = function(
	TargetCharacter: Character, Range: number,
	ArrayToCheck: {[number]: Character}
) : Character?
	if not TargetCharacter or not Range or not ArrayToCheck then
		warn("One argument or more arguments are nil! Ending this function.")
		return nil
	end
	local closestCharacter: Character?
	
	for _,char: Character in ArrayToCheck do
		local distance = (TargetCharacter.PrimaryPart.Position - char.PrimaryPart.Position).Magnitude
		if distance < Range then
			Range = distance
			closestCharacter = char
		end
	end
	return closestCharacter
end,
	["GetAllPlayersCharacters"] = function() : {[number]: Character}
	local characterlist = {}
	
	for _,player in Players:GetPlayers() do
		local character = player.Character :: Character
		if character then
			table.insert(characterlist, character)
		end
	end
	
	return characterlist
end,
}

return MoveUtilities

the script above gives me two warnings:
Screenshot (176)(1)

this line in the script is causing it

local distance = (TargetCharacter.PrimaryPart.Position - char.PrimaryPart.Position).Magnitude

i dont know how to fix this, ive already tried the union type example | nil, example? but seems to have no difference

im using the Strict LuauTypeCheckMode

Are you using strict?

character

yes im using that strict luautypecheckmode

I think the problem is that you are assuming PrimaryPart is something, but in a model PrimaryPart can obviously be null if nothings set so you must verify with a simple if statement.

That’s because Model.PrimaryPart is typed as “BasePart?”. If you know it’s guaranteed that the primary part exists, you can use something called a typecast to force a valid type:

-->> instead of 
TargetCharacter.PrimaryPart.Position

-->> do
(TargetCharacter.PrimaryPart:: BasePart).Position --> now PrimaryPart is treated as a BasePart

But you can avoid this entirely by simply doing:

TargetCharacter:GetPivot().Position --> gets the position of the model

… of which pretty much does the same thing and avoids this problem altogether.

yes but i prefer to use an if statement so i can debug easier

Then use an assert statement. It handles the check and the error reporting in one line without cluttering your logic with extra if blocks.

the problem isnt even about readability

Then what exactly is the problem? You said you’re using an if statement to debug, but you didn’t explain what the actual issue is now.

the actual issue here is already explained the type casting and that one already resolves this

Then what is the issue? You haven’t actually stated what the problem is, just that you’re using an if statement for debugging.

read the topic again and the problem is there

I have. You just said the type casting resolves it, which is exactly what was suggested in post #5. If that fixed your issue, there is nothing left to discuss.