I am making an scpf game and a script which takes over the player and attacks other players around it
It is getting a timeout when trying to get the closest player causing it to stop altogether and do nothing
I have tried adding a delay but that only makes it slow to update on the targets postion
Here is the code:
local pathFindingService = game:GetService("PathfindingService")
game.ReplicatedStorage.Networking.SCPs.ToggleControls:FireClient(game.Players:GetPlayerFromCharacter(script.Parent), false)
for i, v in pairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") == true then
v.Touched:Connect(function(thing)
if thing.Parent:FindFirstChildOfClass("Humanoid") ~= nil then
if thing.Parent ~= script.SCP.Value then
thing.Parent.Humanoid:TakeDamage(25)
end
end
end)
end
end
repeat
local closestDistance = 0
local closestCharacter = nil
for i, v in pairs(game.Players:GetPlayers()) do
if v.Character ~= nil and v.Character ~= script.Parent and v.Character.Humanoid.Health ~= 0 and v.Character:FindFirstChild("Zombify") == nil then
local path = pathFindingService:CreatePath()
path:ComputeAsync(script.Parent.PrimaryPart.Position, v.Character.PrimaryPart.Position)
if path.Status == Enum.PathStatus.Success then
local distance = (script.Parent.PrimaryPart.Position - v.Character.PrimaryPart.Position).Magnitude
if distance <= 50 and (closestCharacter == nil or distance < closestDistance) then
closestDistance = distance
closestCharacter = v.Character
end
end
end
end
if closestCharacter ~= nil then
local path = pathFindingService:CreatePath()
path:ComputeAsync(script.Parent.PrimaryPart.Position, closestCharacter.PrimaryPart.Position)
local waypoints = path:GetWaypoints()
if waypoints[3] ~= nil then
script.Parent.Humanoid:MoveTo(waypoints[3].Position)
if waypoints[3].Action == Enum.PathWaypointAction.Jump then
script.Parent.Humanoid.Jump = true
end
else
script.Parent.Humanoid:MoveTo(closestCharacter.PrimaryPart.Position)
end
end
until script.Parent.Humanoid.Health == 0
game.ReplicatedStorage.Networking.SCPs.ToggleControls:FireClient(game.Players:GetPlayerFromCharacter(script.Parent), true)
Please let me know on anything, thanks!
