Error "Unable to cast to Dictionary" on AI

Hello! I’m currently having a bug with my AI, I’m planning to make some kind of “RAKE” game but my AI is having the issue “Unable to cast to Dictionary” and I have no idea how do I fix it.

local replicatedStorage = game:GetService("ServerScriptService")
local runService = game:GetService("RunService")

local rake = script.Parent

local humanoid = rake:FindFirstChildWhichIsA("Humanoid") or rake:WaitForChild("NPC")
local animator = humanoid:WaitForChild("Animator")
local rootPart = rake:WaitForChild("HumanoidRootPart")
local head = rake:WaitForChild("Head")

local checkpoints = workspace:WaitForChild("Checkpoints"):GetChildren()

local sounds = {}

local animations = {}

local config = rake:WaitForChild("Configuration")
local animObjects = rake:WaitForChild("Animations")

local pathfinder = game:GetService("PathfindingService")

local pathParameters = {
	AgentHeight = 5,
	AgentRadius = 2,
	AgentCanJump = true,
	AgentCanClimb = true,
	Costs = {
		Climb = 2
	}
}

local path = pathfinder:CreatePath(rake,pathParameters)

local targetPosition = Vector3.new()

local chaseTarget: Model? = nil
local wanderTarget: Model? = nil

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {rake}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

humanoid:NetworkOwner(nil)

-- Times

local wanderDelay = time()
local swingDelay = time()

local checkpointCheck = false

local currentCheckpoint = 1

for _,sound: Sound in pairs(rake:GetDescendants()) do
	if sound:IsA("Sound") then
		sounds[sound.Name] = sound
	end
end


for _,animObj: Animation in pairs(rake:GetDescendants()) do
	if animObj:IsA("Animation") and (animObj.AnimationId ~= nil or animObj.AnimationId ~= "") then
		pcall(function()
			sounds[animObj.Name] = animator:LoadAnimation(animObj)
		end)
	end
end



local function playSound(name)
	if sounds[name] then
		sounds[name]:Play()
	end
end

local function playAnim(name,ignoreIsPlaying)
	if animations[name] then
		local playAnim = not animations[name].IsPlaying

		if ignoreIsPlaying then
			playAnim = true
		end

		if playAnim then return end
		animations[name]:Play()
	end
end

local function getTarget(maxDistance: number): Model?
	local targetDistance = math.huge
	local target: Model? = nil

	for _,character: Model in pairs(workspace:GetChildren()) do
		if character == rake then continue end
		if not character:IsA("Model") then continue end

		local targetRootPart = character:FindFirstChild("HumanoidRootPart")
		local targetHumanoid = character:FindFirstChildWhichIsA("Humanoid")


		if targetRootPart and (targetHumanoid and targetHumanoid.Health > 0) then
			local distance = (targetRootPart.Position - rootPart.Position).Magnitude
			if distance <= maxDistance and distance <= targetDistance then
				target = character
				targetDistance = distance
			end
		end
	end

	return target
end



local function chasing() --Fires when Rake enters chasing state
	script.Parent.Head.RakeScream:Play()
	local anim = animator:LoadAnimation(animObjects.Scream)
	anim:Play()
end

local function wandering() --Fires when Rake begins to wander
	targetPosition = Vector3.new((rootPart.Position.X+math.random(-100,100)),rootPart.Position.Y,(rootPart.Position.Z+math.random(-100,100)))
end

rake.DescendantAdded:Connect(function(sound)
	if sound:IsA("Sound") then
		sounds[sound.Name] = sound
	end
end)

runService.Heartbeat:Connect(function(dt)
	path:Run(targetPosition)
	chaseTarget = getTarget(config.NoticeDistance.Value)
	wanderTarget = getTarget(math.huge)

	local slideRay: RaycastResult = workspace:Raycast(rootPart.Position,Vector3.new(0,7,0),raycastParams)

	if slideRay then
		local hit: BasePart = slideRay.Instance
		local model = hit:FindFirstAncestorWhichIsA("Model")
		local humanoid = model and model:FindFirstChildWhichIsA("Humanoid")
		if model and humanoid then
			local t_rootPart: BasePart = model:FindFirstChild("HumanoidRootPart")
			if t_rootPart then
				print("pushing")
				t_rootPart.AssemblyLinearVelocity = t_rootPart.CFrame.LookVector * (rootPart:GetMass() * 10)
			end
		end
	end

	if chaseTarget then
		local targetRootPart = chaseTarget:FindFirstChild("HumanoidRootPart")
		targetPosition = targetRootPart.Position + targetRootPart.CFrame.LookVector * 2.5

		if (targetRootPart.Position - rootPart.Position).Magnitude < 5 then
			humanoid.AutoRotate = false
			local _,rotY,_ = CFrame.lookAt(rootPart.Position,targetRootPart.Position):ToOrientation()
			rootPart.CFrame = rootPart.CFrame:Lerp(CFrame.new(rootPart.Position) * CFrame.Angles(0,rotY,0),0.25)
		else
			humanoid.AutoRotate = true
		end

		if (animations["Scream"] and not animations["Scream"].IsPlaying) or not animations["Scream"] then
			humanoid.WalkSpeed = config.RunSpeed.Value
		else
			humanoid.WalkSpeed = 0
		end
		if not config.IsChasing.Value then
			chasing()
			config.IsChasing.Value = true
		end
		--[[
		if (time() - swingDelay) > 1 then
			swing()
			swingDelay = time()
		end
		--]]
	elseif wanderTarget then
		humanoid.AutoRotate = true
		local targetRootPart = wanderTarget:FindFirstChild("HumanoidRootPart")
		humanoid.WalkSpeed = config.WalkSpeed.Value
		if (time() - wanderDelay) > 1 then
			if not checkpointCheck then
				targetPosition = Vector3.new((rootPart.Position.X+math.random(-100,100)),rootPart.Position.Y,(rootPart.Position.Z+math.random(-100,100)))
			end
			wanderDelay = time()
		end

		if checkpointCheck then
			targetPosition = checkpoints[currentCheckpoint].Position
			if (checkpoints[currentCheckpoint].Position - rootPart.Position).Magnitude <= 5 then
				local newCheckpoint = math.random(1,#checkpoints)
				if newCheckpoint ~= currentCheckpoint then
					currentCheckpoint = newCheckpoint
				end
			end
		end


		if config.IsChasing.Value == false then
			wandering()
		end

		if config.IsChasing.Value then
			wandering()
			config.IsChasing.Value = false
		end
	end
end)

If anyone could tell me what do I do, it would be great.

That’s a very long script! Could you point out what line the error appears on?

Create path should only have the table as its parameters

You should look at documentation first.

Also you can search up the error message along with the keyword “pathfinding” to get the below post with a similar error message:

Thanks, that fixed the problem but now my problem has completely changed.

When the target runs out of his NoticeDistance, he keeps twitching, wandering and going at the target at the same time. No errors this time.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.