Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My Follow script is moving the NPC around and around in circles, why?

Asked by 3 years ago
Edited 3 years ago

This script is made to handle multiple players fighting a Mob, but it's not working. they are just moving around and around in circles. Please help.

Code:

local UpdatePosition = 0.2 --How long it takes target the next humanoid
local DamageTime = 3 --How long it takes for the enemy to attack again
local Damage = 30 --How much damage the enemy does
--Advanced Config
local AgroDist = 100 --The amount of studs that the enemy can find you at
local HitBox = script.Parent.HitBox --Change to the part that it has to touch to deal damage (example: script.Parent.Head, script.Parent.LeftArm. Make sure it's not the rootpart though)
-- Do not edit the below unless you know what you are doing
local Player = game.Players.LocalPlayer
local enhum = script.Parent.Enemy
local ddb = false
local character
local ClosestMagnitude
local ClosestMagnitudeObject
-- This is the real script. Only edit if you know what you are doing
function Update(UpdTime)
    for _, v in pairs(game.Workspace:GetChildren()) do
        if v:FindFirstChild("Humanoid") then
            if ClosestMagnitude and ClosestMagnitudeObject and not ddb then
                ddb = true
                character = v
                if (character.Humanoid.RootPart.Position - enhum.RootPart.Position).Magnitude < ClosestMagnitude and (character.Humanoid.RootPart.Position - enhum.RootPart.Position).Magnitude <= AgroDist then
                    if ClosestMagnitudeObject and ClosestMagnitudeObject.Humanoid and ClosestMagnitude["Humanoid"].RootPart then
                        if (character.Humanoid.RootPart.Position - ClosestMagnitudeObject.Humanoid.RootPart.Position).Magnitude <= ClosestMagnitude  then
                            wait(UpdTime)
                            ddb  = false
                            ClosestMagnitudeObject = v
                            ClosestMagnitude = (character.Humanoid.RootPart.Position - enhum.RootPart.Position).Magnitude
                            enhum:MoveTo(v.Humanoid.RootPart.Position)
                        end
                    end 
                end 
            elseif not ddb then
                ddb = true
                character = v
                if (character.Humanoid.RootPart.Position - enhum.RootPart.Position).Magnitude <= AgroDist then
                    wait(UpdTime)
                    ddb  = false
                    ClosestMagnitude = (character.Humanoid.RootPart.Position - enhum.RootPart.Position).Magnitude
                    enhum:MoveTo(v.Humanoid.RootPart.Position)
                end
            end  
        end
    end
end
while wait() do
    Update(UpdatePosition)  
end

Please help!

Answer this question