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

My pathfinding bot stays still for 10 seconds after a player turns a corner. Help?

Asked by 2 years ago
Edited 2 years ago

Hello there! I'm having trouble with my pathfinding bot here. After a player turns a corner the bots stops moving for 10 seconds. It is really irritating. Here's the script.

local myHuman = script.Parent:WaitForChild("MappingBot")
local myChar = script.Parent
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local myHead = script.Parent:WaitForChild("Head")
local tweenService = game:GetService("TweenService")
local curTarget


function findTarget()
    local target
    local dist = 100000
    for i,v in pairs(workspace:GetChildren()) do
        local human = v:FindFirstChild("Humanoid")
        local rootPart = v:FindFirstChild("HumanoidRootPart")
        if human and rootPart and v ~= script.Parent then
            if (myRoot.Position - rootPart.Position).magnitude <= dist and human.Health > 0 then
                dist = (myRoot.Position - rootPart.Position).magnitude
                target = rootPart
            end
        end
    end

    return target
end

function checkSight(target)
    local ray = Ray.new(myHead.Position,(target.Position - myHead.Position).Unit * 1000)
    local ignorelist = {myChar}
    local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, ignorelist)

    if hit then
        if hit:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") then
            if math.abs(hit.Position.Y - myRoot.Position.Y) < 2 then
                return true
            else
                return false
            end
        else
            return false
        end
    else
        return false
    end
end

local doorDebounce = true
myHuman.Touched:Connect(function(obj)
    if obj.Name == "DoorFrame" and doorDebounce == true then
        doorDebounce = false
        local opened = obj.Parent:FindFirstChild("Opened")
        local hinge = obj.Parent:FindFirstChild("Hinge")
        local hingeOpened = obj.Parent:FindFirstChild("HingeOpened")
        local doorLock = obj:FindFirstChild("DoorLock")
        local doorOpen = obj:FindFirstChild("DoorOpen")
        if opened and hinge and hingeOpened and doorLock and doorOpen then
            if opened.Value == false then
                doorLock:Play()
                opened.Value = true

                myHuman.WalkSpeed = 0
                obj.CanCollide = false

                wait(.25)

                doorOpen:Play()
                tweenService:Create(hinge,TweenInfo.new(.35,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{CFrame = hingeOpened.CFrame}):Play()
                wait(.45)

                coroutine.resume(coroutine.create(function()
                    wait(2)
                    obj.CanCollide = true
                end))

                myHuman.WalkSpeed = 14
            end
        end
    end

    doorDebounce = true
end)

while true do
    local target = findTarget()
    if target then
        local sight = checkSight(target)
        if sight == true then
            repeat wait()
                myHuman:MoveTo(target.Position)
            until checkSight(target) == false
        elseif sight == false then
            local path = game:GetService("PathfindingService"):CreatePath()
            path:ComputeAsync(myRoot.Position,target.Position)

            if path.Status == Enum.PathStatus.Success then
                for i,v in pairs(path:GetWaypoints()) do
                    myHuman:MoveTo(v.Position)

                    if v.Action == Enum.PathWaypointAction.Jump then
                        myHuman.Jump = true
                    end

                    if checkSight(target) == true then
                        break
                    end

                    local timeOut = myHuman.MoveToFinished:Wait(.5)

                    if not timeOut then
                        break
                    end
                end
            else
                local door
                local dist = 1000

                for i,v in pairs(workspace:GetDescendants()) do
                    local doorFrame = v:FindFirstChild("DoorFrame")
                    if doorFrame then
                        local doorClose = doorFrame:FindFirstChild("DoorClose")
                        local opened = v:FindFirstChild("Opened")
                        if opened and doorClose then
                            if (myRoot.Position - doorFrame.Position).magnitude <= dist then
                                dist = (myRoot.Position - doorFrame.Position).magnitude
                                door = doorFrame
                            end
                        end
                    end
                end

                if door then
                    local path = game:GetService("PathfindingService"):CreatePath()
                    path:ComputeAsync(myRoot.Position,door.Position)

                    if path.Status == Enum.PathStatus.Success then
                        for i,v in pairs(path:GetWaypoints()) do
                            myHuman:MoveTo(v.Position)

                            if v.Action == Enum.PathWaypointAction.Jump then
                                myHuman.Jump = true
                            end

                            if checkSight(target) == true then
                                break
                            end

                            local timeOut = myHuman.MoveToFinished:Wait(.5)

                            if not timeOut then
                                break
                            end
                        end
                    end
                end
            end
        end
    end

    wait(.25)
end

I have tried that if the move direction of the humanoid is 0, 0, 0 it disables the script then undisables it, but it dosen't work at all. Please help.

the piece of code i think is not working that well:

while true do
    local target = findTarget()
    if target then
        local sight = checkSight(target)
        if sight == true then
            repeat wait()
                myHuman:MoveTo(target.Position)
            until checkSight(target) == false
        elseif sight == false then
            local path = game:GetService("PathfindingService"):CreatePath()
            path:ComputeAsync(myRoot.Position,target.Position)

            if path.Status == Enum.PathStatus.Success then
                for i,v in pairs(path:GetWaypoints()) do
                    myHuman:MoveTo(v.Position)

                    if v.Action == Enum.PathWaypointAction.Jump then
                        myHuman.Jump = true
                    end

                    if checkSight(target) == true then
                        break
                    end

                    local timeOut = myHuman.MoveToFinished:Wait(.5)

                    if not timeOut then
                        break
                    end
                end
0
Hi! Is all 159 lines neccery for people to see? Can you just post the chunk which you thought was wrong? Thanks! Xyternal 247 — 2y
0
Sure! Oliver_Bocch 32 — 2y
0
Hey, do you want it to happen all at once? If so, then coroutines might be your answer. JesseSong 3916 — 2y

Answer this question