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
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!!!
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.
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…