"attempt to call a nil value"

  1. What do you want to achieve?
    Have the humanoid be able to recalculate the path when the targets position changes

  2. What is the issue?
    the code makes this strange error on a function that should work 100% of the time. This issue occurs when the script realizes that the position of the target has changed and attempts to call the “walktopath” function which usually works. All parameters are valid to my knowledge.

  3. What solutions have you tried so far?
    I’ve checked the developer forum for answers and my summary is “something is setting the function used to nil”. Also tried tinkering with the code a bit though it doesn’t seem to change anything.

The code:

local function walktopath(points,hum,part)
	local oldpos = nil
	if part then
		oldpos = part.Position
	end
	for i,v in pairs(points) do
		hum:WaitForChild("Humanoid"):MoveTo(v.Position)
		hum:WaitForChild("Humanoid").MoveToFinished:Wait()
		if part then
			if part.Position ~= oldpos then
				makepath(hum,part)
				break
			end
		end
	end
end

local function makepath(model,destination)
	local path = nil
	repeat
		path = game:GetService("PathfindingService"):CreatePath({AgentRadius = 2, AgentHeight = 0,AgentCanJump = false})  
		if typeof(destination) == "Instance" then
			local s = destination
			path:ComputeAsync(model:WaitForChild("HumanoidRootPart").Position,s.Position)
		else
			path:ComputeAsync(model:WaitForChild("HumanoidRootPart").Position,destination)
		end
	until path.Status == Enum.PathStatus.Success
	walktopath(path:GetWaypoints(),model,(typeof(destination) == "Instance" and destination)) -- errors here
end

makepath(workspace:WaitForChild("char"),workspace:WaitForChild("point"))

This post was flagged by the community and is temporarily hidden.

Unfortunately that doesn’t seem to change anything

This post was flagged by the community and is temporarily hidden.

1 Like

Thanks for the solution!
I never would’ve thought of that myself

1 Like