SCP's don't follow the players correctly

Hello, developers. I have made a script that controls the scps movement so all of the scps only have to run from one single follow script. However, it seems like this follow script is not working because when 2 or more players are in the distance range of the scp, the scps go back and forth to the player, essentially making them stuck.

here is a video of that happening:

and here is the code:

-- 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

		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

-- Loop

while true do
	task.wait(SCPGlobal.LoopTime)
	for _,v in ControllableSCPS:GetChildren() do
		if v:FindFirstChild(v.Name.."AI") then
			v = v:FindFirstChild(v.Name.."AI")
		end
		
		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
2 Likes

make it check if there’s an existing target if there is don’t change and if there isn’t then find nearest torso

1 Like

This works, but I want it to switch always to the nearest torso.

-- 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

		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
							
							pcall(function()
								if scp:FindFirstChild("SCPData"):FindFirstChild("Target").Value ~= "" then
									return false
								end
							end)

							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

-- Loop

while true do
	task.wait(SCPGlobal.LoopTime)
	for _,v in ControllableSCPS:GetChildren() do
		if v:FindFirstChild(v.Name.."AI") then
			v = v:FindFirstChild(v.Name.."AI")
		end

		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)
				v:FindFirstChild("SCPData"):FindFirstChild("Target").Value = Target.Name
			elseif Target == nil then
				pcall(function()
					v:FindFirstChild("SCPData"):FindFirstChild("Target").Value = ""
				end)
			end
		end
	end
end

What’s an SCP? Guesswork here… Script Controlled Players, Sus Car Police? Stagnant Carp Pools? Sometimes acronyms are the least descriptive type of language to use in a forum with very distinct rules on the description of the question!!!

An SCP is just a enemy name
its not an acronym or anything (well it is but it isnt relevant to this code here)

Seems like your code gets the distance furthest away from the player. By default, table.sort sorts the table in ascending order. This means that the following code gets the distance furthest away in the table.

local nearestDist = playerDists[#playerDists]

Change it to the first index [1].

1 Like

SCP stands for Sus Car Police that is correct

3 Likes

Like Sore stated, an SCP is an enemy which you can find out by looking at the functions name and the video which I posted so people could watch my problem I was facing.

Okay cool. Being the first word (capitalised too) in the title of the thread, tends to make one think it is the most important and prominent of all the words contained in the question posed…

I will do this and tell you how it goes, thanks!

1 Like

then do if the current target is farther than any other target then switch to the nearest

Understandable, but realistically, the title basically means " NPCS’s don’t follow the players correctly

Thank you! This works perfectly as I intended it to work. I must have overlooked this line.

1 Like

well to be fair, most people know what scp’s are, in his defense ofc

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.