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

Enemies wont follow me when i use MoveTo humanoid function?

Asked by 5 years ago

Basically I'm trying to integrate the follow script into a short and con sized form for less lag and ease of access. Basically when I spawn an enemy, I spawn a function to follow. The enemy picks up my humanoid and prints my players name after the "MoveTo" function has started. However the MoveTo for some reason doesn't work and doesn't want to move the enemy. The weird part is there is new errors.

Heres the code;

-- Server Script for spawning ai

local AiModule = require(script:WaitForChild("AiModule"))
local ServerStorage = game:GetService("ServerStorage")

local AiId = 0

local playerData = {}

local AiCollection = ServerStorage:WaitForChild("AiCollection"):GetChildren()

function findNearestTorso(pos, Ai)
    local list = game.Workspace:GetChildren()
    local torso = nil
    local dist = 100
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") then
            temp = temp2:findFirstChild("Torso")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

for i = 1, #AiCollection do
    if AiCollection[i]:FindFirstChild("Human") ~= nil then
        wait(1)
        AiId = AiId + 1
        AiCollection[i].Name = AiCollection[i].Name .. "_" .. AiId
        AiModule.RunTest(AiCollection[i], AiId)
        AiModule.SpawnAi(AiCollection[i])

        local AiIdValue = ServerStorage:WaitForChild("AiServerStatCollection"):WaitForChild(AiCollection[i].Name .. "Folder"):WaitForChild("AiId") 
        AiIdValue.Value = AiId

        spawn(function()
            while true do
                wait(5)
                local target = findNearestTorso(AiCollection[i]:WaitForChild("Torso").Position, AiCollection[i])
                if target ~= nil then
                    for i, Player in pairs (game.Players:GetChildren()) do
                        AiCollection[i]:WaitForChild("Human"):MoveTo(target.Position ,target)
                        print(target.Parent)
                    end
                end
            end
        end)
    end
end
0
I assume you've already checked if every part in the enemy was unanchored? mattscy 3725 — 5y
0
Correct and it was. Kaosinfusedknight 36 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

It could be an infinite yield from the waitForChild. WaitForChild is typically used in local scripts, to make the script resilient to loading issues. Either add the timeout parameter, or change it to FindFirstChild. Also, make sure everything exists by using FindFirstChild ~= nil.

Ad

Answer this question