Click to move script bugging out on moving objects

I have a pathfinding script that I am using from the dev hub (or wiki i dunno whats its called anymore lol) and when I try moving towards a moving object, when it updates the path, the character moves backwards before moving forwards, so instead of ever progressing the character just spazzes out in place.

Ex:

https://gyazo.com/5113ecf682440c2075ce233ad7331faa

Could we see the script that moves your character? It’d help :slightly_smiling_face:

The script is apart of a giant script, so I’ll just grab the actual walking part of it. I just fire the function move() with the position when I want to move to it.

local plr = game.Players.LocalPlayer
local chr = script.Parent
local hr = chr.HumanoidRootPart
local h = chr.Humanoid

curpa = nil

wp = {}
cwpi = 0
walktopoint = hr.Position

local function move(pos)
	local pfs = game:GetService("PathfindingService")

	local pathparams = {
		AgentHeight = 5,
		AgentRadius = 4,
		AgentCanJump = false
	}

	local succ,msg = pcall(function()
		local path = pfs:CreatePath(pathparams)

		path:ComputeAsync(hr.Position,pos)

		curpa = path

		if path.Status == Enum.PathStatus.Success then

			wp = path:GetWaypoints()

			cwpi = 1

			h:MoveTo(wp[cwpi].Position)

		else

			h:MoveTo(hr.Position)

		end

		curpa.Blocked:connect(onpathblocked)

	end)

	if not succ then
		warn(msg)
	end
end

function onwaypointreached(reached)
	if reached and cwpi < #wp then
		cwpi += 1
		h:MoveTo(wp[cwpi].Position)
	end
end

function onpathblocked(blocked)
	if blocked > cwpi then
		move(walktopoint)
	end
end

-- movestuffs

h.MoveToFinished:connect(onwaypointreached)

It is probably because if you click when you are already moving, then the game is trying to move you to two different positions at once. Maybe try replacing the move function with this? I gotta go so if this doesn’t work then I can fix it later:

local function move(pos)
	local pfs = game:GetService("PathfindingService")

	local pathparams = {
		AgentHeight = 5,
		AgentRadius = 4,
		AgentCanJump = false
	}

	local succ,msg = pcall(function()
		local path = pfs:CreatePath(pathparams)

		path:ComputeAsync(hr.Position,pos)

		curpa = path
                 h.MoveToFinished:Wait()

		if path.Status == Enum.PathStatus.Success then

			wp = path:GetWaypoints()

			cwpi = 1

			h:MoveTo(wp[cwpi].Position)

		else

			h:MoveTo(hr.Position)

		end

		curpa.Blocked:connect(onpathblocked)

	end)

	if not succ then
		warn(msg)
	end
end

The way it’s set up it runs off the global “wp” so it won’t ever move to multiple places at once. Plus the only thing looping it is the event “MoveToFinished” it’s not that. Its when I update it my character appears to move to a point behind him, but I can’t figure out a fluent way to get him to move to a point in front of him instead so he doesn’t spazz in place.

After what I said, I went to tinker and changed setting the cwpi from 0 to this:

cwpi = math.floor(#wp*.3)

It stops me from moving backwards and it works now.

1 Like