How to find nearest player only if they are not the "king"

There was no way of explaining this in a title.

I have a function that can detect the nearest player from a block. Although, most of the time the nearest player from that block will be the “king” that controls the block. How can I make this function detect the nearest player that is not the “king”?

Note: ReplicatedStorage has a string value that says which player is the “king”, it changes when someone else becomes king.

Here is the function:

local function FindNearestPlayer(position)
	local found
	local last = math.huge
	for _,plyr in pairs(game.Players:GetPlayers()) do
		local distance = plyr:DistanceFromCharacter(position)
		if distance < last then
			found = plyr.Name
			last = distance
	return found

Thanks for any help! :smiley:

You can just modify the if-statement inside the loop:

if distance < last and game.ReplicatedStorage.King.Value ~= plyr.Name then
1 Like

Oh haha I thought that wouldn’t work because it would find that the nearest player is the king and then it would stop, but I was wrong. It works perfect! Thanks :slight_smile: