Cloned enemy doing nothing

The main gist of this script is to create about 11 clones of the original model within replicated storage, and have the pathfinding for the player ready. Enemy is being cloned, but not doing anything.

I’ve tried activating everything once a player’s character is added, completely erasing everything and building the script back up from scratch but that didn’t work, I put the dummy in replicatedstorage instead of serverstrorage, but no luck there either.

Here is the code:

local GetPlayers = players:GetPlayers() 
local pathfinding = game:GetService("PathfindingService") 
local runService = game:GetService("RunService")
local humanoid = script.Parent.Humanoid
local humanoidRootPart = script.Parent.HumanoidRootPart
local path

local pathparams = {
	AgentHeight = 5, 
	AgentRadius = 3, 
	AgentCanJump = true 
} 

while humanoidRootPart.Parent ~= game.Workspace do 
	wait()
end
humanoidRootPart:SetNetworkOwner(nil)

local function findNearestTarget()
    local nearest 
    local distance
    
    for _, player in pairs(GetPlayers) do
        local character = player.Character
        local PhumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        local Phumanoid
        if character then 
            distance = (humanoidRootPart.Position - PhumanoidRootPart.Position).Magnitude 
            Phumanoid = character:FindFirstChild("Humanoid") 
            if distance > 60 and Phumanoid.Health > 0 then 
                nearest = player
            end
            return nearest, PhumanoidRootPart
        end
    end
end 

local nearest, PhumanoidRootPart = findNearestTarget() 

runService.Heartbeat:Connect(function()
    if nearest then 
        local success, errorM = pcall(function()
            path = pathfinding:CreatePath(pathparams)
            path:ComputeAsync(humanoidRootPart.Position, PhumanoidRootPart.Position)
        end)
        if success then 
        local waypoints = path:GetWaypoints() 
        for _, w in pairs(waypoints:GetChildren()) do 
                humanoid:MoveTo(w.Position)
                humanoid.MoveToFinished:Wait(2) 
            end
        else 
            warn("Somethings wrong...", errorM)
        end
    end
end) ```

Make sure to add

to the loop otherwise its checking 1 time then not ever checking again

1 Like

in the loop, yet the same problem, the issue is that its not even starting the script, not to mention even if it detected the nearest player it wouldnt do anything

Remove this and put

repeat task.wait() until humanoidRootPart.Parent ~= game.Workspace

Anything after a while loop wont run this would be why your script “Isnt running” if this isn’t it then please tell me

Make sure to keep the function call inside the loop still, Also you can put prints along the way of the script to see how far it gets and at what point it fails

Well, they move now. But wait a while (probably because of the repeat until loop), walk to me, and jitter around, then stand still after walking in my general direction, I move away again and come back and they do it again, walk in my general direction, in one straight line, then dont move again until i exit and re-enter their range

1 Like

Here’s the problem in action


and the current script:

local GetPlayers = players:GetPlayers() 
local pathfinding = game:GetService("PathfindingService") 
local runService = game:GetService("RunService")
local humanoid = script.Parent.Humanoid
local humanoidRootPart = script.Parent.HumanoidRootPart
local path

local pathparams = {
	AgentHeight = 5, 
	AgentRadius = 3, 
	AgentCanJump = true 
} 

print("1 - 15 functional")
repeat task.wait() until humanoidRootPart.Parent ~= game.Workspace
print("script continues...")

humanoidRootPart:SetNetworkOwner(nil) 

local function findNearestTarget()
    local nearest 
    local distance
    
    for _, player in pairs(GetPlayers) do
        local character = player.Character
        local PhumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        local Phumanoid
        if character then 
            distance = (humanoidRootPart.Position - PhumanoidRootPart.Position).Magnitude 
            Phumanoid = character:FindFirstChild("Humanoid") 
            if distance > 60 and Phumanoid.Health > 0 then 
                nearest = player
            end
            return nearest, PhumanoidRootPart
        end
    end
end 


runService.Heartbeat:Connect(function()
    local nearest, PhumanoidRootPart = findNearestTarget() 
    if nearest then 
        local success, errorM = pcall(function()
            path = pathfinding:CreatePath(pathparams)
            path:ComputeAsync(humanoidRootPart.Position, PhumanoidRootPart.Position)
        end)
        if success then 
        local waypoints = path:GetWaypoints() 
        for _, w in pairs(waypoints) do 
                humanoid:MoveTo(w.Position)
                humanoid.MoveToFinished:Wait(2) 
            end
        else 
            warn("Somethings wrong...", errorM)
        end
    end
end)```
1 Like

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