Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

2 comments

0
0

That doesent change anything about this particular yield

2 answers

✓ Accepted answer

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

6 comments

0

I assume im supposed to put the complete for loop in FollowPath instead of just the repeat?

0

Whoops, yeah that's my bad

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.

0

With your edit added the droid moves but it still doesent run every 2.8 seconds

0

There are quite a few errors in that script, take a look at this set-up: https://code.stypi.com/mastajames/Pathfinding.lua

0

Alright we did a lot of debugging and managed to get this working

#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

3 comments

0

So that would put the repeat loop inside of a spawned function?

0

Yep! It would be almost as if the repeat loop did not exist, but yet it still does and it will still run

0

Wouldnt that mean the for loop would run instantly before the while loop does?

This discussion is preserved as it was. New answers, comments, and votes are closed.