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

Could I get some help with tweaking this script's yielding?

Asked by
drew1017 330 Moderation Voter
8 years ago
while wait(2.8) do
    path = PFS:ComputeSmoothPathAsync(Droid.Head.Position, Droid.Head.PathTarget.Position, 512) -- raw
    pathc = PFS:ComputeSmoothPathAsync(Droid.Head.Position, Droid.Head.PathTarget.Position, 512):GetPointCoordinates()
    for i,v in pairs(pathc) do
        local Part = Instance.new("Part", Droid.Head)
        Part.CFrame=CFrame.new(v)
        Part.Transparency = 0
        Part.Color = Color3.new(0.2, 0, 1)
        Part.Size = Vector3.new(1, 1, 1)
        Part.Anchored = true
        Part.CanCollide = false
        Part.Locked = true
        Part.Name = 'Path'
        Part.Touched:connect(function(hit)
            Part:Destroy()
        end)
            if path.Status == Enum.PathStatus.Success then
                Part.Color = Color3.new(0, 1, 0) -- green
            elseif path.Status == Enum.PathStatus.ClosestNoPath then
                Part.Color = Color3.new(1, 0, 0) -- red
            elseif path.Status == Enum.PathStatus.ClosestOutOfRange then
                Part.Color = Color3.new(0.5, 0.75, 0) -- yellow
            elseif path.Status == Enum.PathStatus.FailStartNotEmpty then
                Part.Color = Color3.new(0.5, 0, 0.5) -- purple
            elseif path.Status == Enum.PathStatus.FailFinishNotEmpty then
                Part.Color = Color3.new(0.5, 0, 0.5) -- purple
            end
    Debris:AddItem(Part, 20.8)
    end
for i, v in pairs(pathc) do
    Humanoid:MoveTo(v)
        repeat
            distance = (v - Droid.Head.Position).magnitude
            wait()
        until distance < 2
    end
end

Basically what the above script does is compute a path, make a part trail over it, and use a for loop + repeat loop to make a droid NPC follow the path.

The problem is the repeat loop yields the entire while loop, so it wont try to compute another path until it's gotten to the target of the first one.

This'll sound very requesty, but bear with me as I have no idea how to do this without breaking it.

What i need it to do is be able to make the droid stop following the path it's on without breaking the loop, compute a new path to PathTarget, then make the droid follow the new path.

0
This might help, use this instead of looping: http://wiki.roblox.com/index.php?title=API:Class/Humanoid/MoveToFinished EzraNehemiah_TF2 3552 — 8y
0
That doesent change anything about this particular yield drew1017 330 — 8y

2 answers

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

What you can do is store the path you want it to be following in a variable. When the path changes, it will overwrite the other path. You can utilize this to compare the path the FollowPath function is using with the desired path and break the loop if they do not match.

local currentPath

local function FollowPath(path, coordinates)
    for i, v in pairs(coordinates) do
        Droid.Humanoid:MoveTo(v)
        local distance
        repeat
            if path ~= currentPath then
                break -- If there is a new path, break this loop
            end
            distance = (v - Droid.Head.Position).magnitude
            wait()
        until distance < 2
    end
end

while wait(2.8) do
    path = PFS:ComputeSmoothPathAsync(Droid.Head.Position, Droid.Head.PathTarget.Position, 512)
    currentPath = path
    coordinates = currentPath:GetPointCoordinates()

    -- Your Path Visualization Code

    spawn(function() FollowPath(path, coordinates) end)
end
0
I assume im supposed to put the complete for loop in FollowPath instead of just the repeat? drew1017 330 — 8y
0
Whoops, yeah that's my bad BlackJPI 2658 — 8y
0
http://pastebin.com/4fHzVM7W <-- With this setup, the while loop still does not run every 2.8 seconds and the droid doesent move at all. drew1017 330 — 8y
0
With your edit added the droid moves but it still doesent run every 2.8 seconds drew1017 330 — 8y
View all comments (2 more)
0
There are quite a few errors in that script, take a look at this set-up: https://code.stypi.com/mastajames/Pathfinding.lua BlackJPI 2658 — 8y
0
Alright we did a lot of debugging and managed to get this working drew1017 330 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Spawn

There is a neat coroutine method provided by Roblox named Spawn which will run your code on a separate thread, almost like creating a brand new script. Using this, we can make the repeat loop run on a different thread.

while wait(2.8) do
    path = PFS:ComputeSmoothPathAsync(Droid.Head.Position, Droid.Head.PathTarget.Position, 512) -- raw
    pathc = PFS:ComputeSmoothPathAsync(Droid.Head.Position, Droid.Head.PathTarget.Position, 512):GetPointCoordinates()
    for i,v in pairs(pathc) do
        local Part = Instance.new("Part", Droid.Head)
        Part.CFrame=CFrame.new(v)
        Part.Transparency = 0
        Part.Color = Color3.new(0.2, 0, 1)
        Part.Size = Vector3.new(1, 1, 1)
        Part.Anchored = true
        Part.CanCollide = false
        Part.Locked = true
        Part.Name = 'Path'
        Part.Touched:connect(function(hit)
            Part:Destroy()
        end)
            if path.Status == Enum.PathStatus.Success then
                Part.Color = Color3.new(0, 1, 0) -- green
            elseif path.Status == Enum.PathStatus.ClosestNoPath then
                Part.Color = Color3.new(1, 0, 0) -- red
            elseif path.Status == Enum.PathStatus.ClosestOutOfRange then
                Part.Color = Color3.new(0.5, 0.75, 0) -- yellow
            elseif path.Status == Enum.PathStatus.FailStartNotEmpty then
                Part.Color = Color3.new(0.5, 0, 0.5) -- purple
            elseif path.Status == Enum.PathStatus.FailFinishNotEmpty then
                Part.Color = Color3.new(0.5, 0, 0.5) -- purple
            end
    Debris:AddItem(Part, 20.8)
    end
for i, v in pairs(pathc) do
    Humanoid:MoveTo(v)
spawn(function()
        repeat
            distance = (v - Droid.Head.Position).magnitude
            wait()
        until distance < 2
end))
    end
end

0
So that would put the repeat loop inside of a spawned function? drew1017 330 — 8y
0
Yep! It would be almost as if the repeat loop did not exist, but yet it still does and it will still run DigitalVeer 1473 — 8y
0
Wouldnt that mean the for loop would run instantly before the while loop does? drew1017 330 — 8y

Answer this question