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

PathFindingService moving enemy to wrong location?

Asked by 3 years ago
Edited 3 years ago

Hello!

Its me again.

This time a part of my script isnt working. Well, it is working, except, its moving to the wrong location... I have 16 waypoints, all of them the size of 1,1,1, transparency 1, CanCollide off, Anchored true.

Heres the script:

wait(6)

local pathfindingservice = game:GetService("PathfindingService")
local SpawnTime = 3
local SpawnRate = 1

local physicsservice = game:GetService("PhysicsService")
physicsservice:CreateCollisionGroup("Enemies")
physicsservice:CollisionGroupSetCollidable("Enemies","Enemies",false)

while script.Parent.EnemyThings.EnemyExit.EnemyExit.Health.Value > 0 do
    wait(SpawnTime)

    for i = 1, SpawnRate do
        wait()
        local enemy =  game.ReplicatedStorage.Enemies:GetChildren()[math.random(1, #game.ReplicatedStorage.Enemies:GetChildren())]:Clone()
        enemy.Parent = workspace.Enemies
        enemy.HumanoidRootPart.CFrame = workspace.SpawnEnemiesCFrame.CFrame * CFrame.new(0,3,0)
        for _, v in pairs(enemy:GetChildren()) do
            if v:IsA("BasePart") then
                physicsservice:SetPartCollisionGroup(v, "Enemies")
            end
        end
        spawn(function()
            for _, waypoints in pairs(workspace.Waypoints:GetChildren()) do
                local path = pathfindingservice:CreatePath()
                path:ComputeAsync(enemy.HumanoidRootPart.Position,  waypoints.Position)
                local wp = path:GetWaypoints()
                for __, wp2 in pairs(wp) do
                    enemy.Humanoid:MoveTo(wp2.Position)
                    enemy.Humanoid.MoveToFinished:Wait()
                end
            end
        end)
    end
end

Its VERY messy, but you can look at the source code (hover over the script section and press "view) and see it fully. Basically, EnemyExit contains a part with a value called "Health". So while the health value isnt zero, it will continue the script.

"Enemies" is a model inside ReplicatedStorage. It (currently) contains 2 enemies. If you would like more information on the enemies, you can kindly ask and I will provide.

SpawnEnemiesCFrame is a part in the workspace where the enemies will spawn.

The loop (I think its called that) function is just to prevent the enemies from colliding with eachother. I'm pretty sure that doesnt effect anything.

Now heres the problem: the spawn function works fine, and everything works correctly, but, as I mentioned before, it goes to the wrong location. I have no idea why this is happening!

This is a script in the WORKSPACE, and I get no error.

If you want more information, you can kindly ask and I will help.

If this is just a simple fix, sorry for the trouble.

PS: Its a tower defense game. Although I doubt that changes anything.

Answer this question