Loop gets the latest player that joined

Hello, developers. I have made a script that gets the nearest player to the scp, and it makes the scp follow the player. However, if the script finds out another player in the same distance for it to see the player, it will follow the player further from it if the player joined the game after the original player.

It’s confusing, I know. If anyone could help I’d be super happy.

-- Paths

local SCPS = workspace:FindFirstChild("SCPS")
local ControllableSCPS = SCPS:FindFirstChild("ControllableSCPS")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ModuleScripts = ReplicatedStorage:FindFirstChild("ModuleScripts")
local SCPStatistics = require(ModuleScripts:FindFirstChild("SCPStatistics"))

local SCPGlobal = SCPStatistics.Global

-- Variables

local Dist

local Debounce = false

-- Functions


function findNearestTorso(pos, scp)
	if scp ~= nil then
		local List = game:GetService("Players"):GetPlayers()
		local Dist = scp:FindFirstChild("SCPData"):FindFirstChild("DistValue")

		local Torso = nil
		local Temp = nil
		local Temp2 = nil
		local Human = nil

		if #List > 0 then
			for i,v in pairs(List) do
				Temp2 = v.Character
				if Temp2 ~= nil then
					Temp = Temp2:FindFirstChild("HumanoidRootPart") or Temp2:FindFirstChild("Torso")
					Human = Temp2:FindFirstChild("Humanoid")
					if (Temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then
						if (Temp.Position - pos).magnitude <= Dist.Value then
							Torso = Temp
							--	Dist = (Temp.Position - pos).magnitude
						end
					end
				end
			end
		end
		return Torso
	end
	return nil
end

-- Loop

while true do
	wait(SCPGlobal.LoopTime)
	for _,v in pairs(ControllableSCPS:GetChildren()) do
		local Torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
		if Torso then
			local Humanoid = v:FindFirstChildOfClass("Humanoid")
			local Target = findNearestTorso(Torso.Position, v)

			if Target ~= nil then
				Humanoid:MoveTo(Target.Position, Target)
			end
		end
	end
end
1 Like

So Basically you want it to follow the closes player?

Yes, it’s suppose to do that. However, if it finds another player in its radius, it will follow that other player if it was the latest one to join.

function findNearestTorso(pos, scp)
	if scp ~= nil then
		local Dist = scp:FindFirstChild("SCPData"):FindFirstChild("DistValue")
		if #game:GetService("Players"):GetPlayers() > 0 then -- is this really necessary? 
			for _,v in game:GetService("Players"):GetPlayers() do
				local Character = v.Character
				if Character and Character:FindFirstChild("HumanoidRootPart") and Character:FindFirstChildOfClass("Humanoid").Health > 0 then
					if (Character.HumanoidRootPart.Position - pos).Magnitude <= Dist.Value then
						return Character.HumanoidRootPart
					end
				end
			end
		end
	end
	return nil
end

-- Loop

while true do
	task.wait(SCPGlobal.LoopTime)
	for _,v in ControllableSCPS:GetChildren() do
		local Torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
		if Torso then
			local Humanoid = v:FindFirstChildOfClass("Humanoid")
			local Target = findNearestTorso(Torso.Position, v)

			if Target ~= nil then
				Humanoid:MoveTo(Target.Position, Target)
			end
		end
	end
end

The scps still go to the player that had joined the latest.

How about tagging each player with a number when they join.

Then you can check if the nearby player has a larger number.

Something like:

local currentCount = game.ServerScriptService.JoinCounter -- create a NumberValue

local function OnPlayerAdded(Player)
local playerNumber = Instance.new("NumberValue")
	playerNumber.Parent = Player
	playerNumber.Value = currentCount.Value + 1 -- tag player with current count
	currentCount.Value = currentCount.Value + 1 -- update counter on server
end

Seems to me the findNearestTorso doesn’t actually find the nearest torso, but only whichever torso is near enough. Try this instead:

function findNearestTorso(pos, scp)
	if scp ~= nil then
		local List = game:GetService("Players"):GetPlayers()
		local Dist = scp:FindFirstChild("SCPData"):FindFirstChild("DistValue")

		local Torso = nil
		local Temp = nil
		local Temp2 = nil
		local Human = nil
		
		local playerDists = {}
		local playerTorsos = {}
		
		if #List > 0 then
			for i,v in pairs(List) do
				Temp2 = v.Character
				if Temp2 ~= nil then

					Temp = Temp2:FindFirstChild("HumanoidRootPart") or Temp2:FindFirstChild("Torso")
					Human = Temp2:FindFirstChild("Humanoid")

					if (Temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then
						if (Temp.Position - pos).magnitude <= Dist.Value then
							
							local distBetween = (Temp.Position - pos).magnitude
							
							playerTorsos[distBetween] = Temp
							table.insert(playerDists, (distBetween))
							
						end
					end
				end
			end
		end
		
		table.sort(playerDists)
		local nearestDist = playerDists[#playerDists]
		
		return playerTorsos[nearestDist]
		
	end	
end

This also doesn’t work.

Let me show you a clip of it happening so that you can understand my problem I’m having.
You can clearly see it keeps switching the target, even though one player is obviously closer to the scps than the other.

Can somebody please help me in this topic, I am very confused as why this is happening!