Attempt to index nil with "Position"

code:
line that errors: path:ComputeAsync(torso.Position, plrhumanoid.Position)
i think it may be because the variable doesn’t have time to assign.

local pfs = game:GetService("PathfindingService")
local pathparams = {
	["AgentHeight"] = 6.5,
	["AgentRadius"] = 5,
	["AgentCanJump"] = false,
	["WaypointSpacing"] = math.huge
}
local waypoints = {}
local waypointindex = 1
script.Parent.PrimaryPart:SetNetworkOwner(nil)
local torso = script.Parent:WaitForChild("Torso")
local humanoid = script.Parent:WaitForChild("Humanoid")
local root = script.Parent:WaitForChild("HumanoidRootPart")
local anim = script.Parent.JumpscareAnimation
local animation = humanoid:LoadAnimation(anim)
local plrhumanoid
game.Players.PlayerAdded:Connect(function(plr)
	plrhumanoid = plr.Character:WaitForChild("HumanoidRootPart")
end)
root.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if plr then
		script.Parent:WaitForChild("Jumpscare"):Play()
		animation:Play()
		root.CanTouch = false
		root.Anchored = true
		plr.Character:WaitForChild("Humanoid").WalkSpeed = 0
	 	plr.PlayerGui:WaitForChild("RunningScript"):Destroy()
	end
end)
local path
local function Walk()
	script.Parent.PrimaryPart:SetNetworkOwner(nil)
path = pfs:CreatePath(pathparams)
	path:ComputeAsync(torso.Position, plrhumanoid.Position)
	if path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		waypointindex = 2
		humanoid:MoveTo(waypoints[waypointindex].Position)
	end
	path.Blocked:Connect(function(blockedwaypointindex)
		if blockedwaypointindex > waypointindex then
			Walk()
		end
	end)
end
while task.wait() do
	Walk()
	end

The error message is pretty understandable, meaning either the torso or plrhumanoid has no value/nil. Try printing torso and plrhumanoid see which one is nil, or it could be both.

1 Like

it’s the playerhumanoid. charrr

If this is the line that initializes the plrhumanoid it could be because of plr.Character is returning a nil. Because the character has not yet loaded. You might have to do something like this first.

local character = plr.Character or plr.CharacterAdded:Wait()
plrhumanoid = character:WaitForChild("HumanoidRootPart")

same error but they aren’t nil… how strange?

What do you mean by same error but they aren’t nil? That’s unlikely to happen.

i used the print on the .touched event.

I think I understand the issue here. What I can tell from the image is that the function gets fired while the plrhumanoid is still nil. This could be because the Character:Wait() still hasn’t fired, maybe. The only solution I can think of is check if plrhumanoid is not equal to nil. This would be inside the function Walk().

if plrhumanoid == nil then
	return
end