Module Script: Attempt to index nil with my root position

I js want this rig to run via module script, however it doesnt quite work yet

heres the screen shot:

ive tried looking on the dev forum however nothing rlly helped so…

heres the script i currently have in both the module script and the script script

local policemodule = {}

function wander(hum, root)
	
	local area = Vector3.new(root.Position.X + math.random(-30, 30), 0, root.Position.Z + math.random(-30, 30))
	local pathfinding = game:GetService("PathfindingService")
	local path = pathfinding:CreatePath({
		AgentCanJump = true,
		AgentCanClimb = true
	})

	path:ComputeAsync(root.Position, area)

	for i, waypoint in pairs(path:GetWaypoints()) do
		local waypoints = path:GetWaypoints()

		if waypoint.Action == Enum.PathWaypointAction.Jump then
			hum.Jump = true
		end

		hum:MoveTo(waypoint.Position)
		local timeout = hum.MoveToFinished:Wait(1)
		local nearest = findnearest()

		if raycastsight(nearest) then
			policemodule.walktotarget(nearest)
			break
		end

		if not timeout then
			hum.Jump = true
			wander(area)
			break
		end
	end
end

function findnearest(root)

	local maxdist = 300
	local nearest = nil

	for i, v in pairs(workspace:GetChildren()) do
		local vhum = v:FindFirstChild("Humanoid")
		local vroot = v:FindFirstChild("HumanoidRootPart")
		local status = v:FindFirstChild("Type")
		
		
		if vhum and vroot and status.Value ~= "Cop" and vhum.Health > 0 then
			if nearest == nil then
				nearest = vroot
			end
			
			if (root.Position - vroot.Position).magnitude <= maxdist then
				maxdist = (root.Position - vroot.Position).magnitude
				nearest = vroot
			end
		end
	end
	return nearest
end

function policemodule.walktotarget(root, hum, character)

	local nearesttarget = findnearest(root)

	local iseeyou = raycastsight(nearesttarget, character)

	if nearesttarget then
		if nearesttarget.Parent:FindFirstChild("Type").Value == "Criminal" then
			if nearesttarget.Parent:FindFirstChild("Humanoid").Health > 0 then
				if iseeyou then

					local pathfinding = game:GetService("PathfindingService")
					local path = pathfinding:CreatePath({
						AgentCanJump = true,
						AgentCanClimb = true
					})

					path:ComputeAsync(root.Position, nearesttarget.Position)

					for i, waypoint in pairs(path:GetWaypoints()) do
						local waypoints = path:GetWaypoints()

						if waypoint.Action == Enum.PathWaypointAction.Jump then
							hum.Jump = true
						end

						if (root.Position - waypoints[1].Position).magnitude > 40 then
							policemodule.walktotarget(nearesttarget)
							break
						end

						hum:MoveTo(waypoint.Position)
						local timeout = hum.MoveToFinished:Wait(1)

						if (root.Position - nearesttarget.Position).magnitude <  14 then
							local c = root.CFrame * CFrame.new(0, 0, 14)
							hum.AutoRotate = false
							hum:MoveTo(c.Position)
							break
						end

						if (root.Position - nearesttarget.Position).magnitude >  7 then
							local c = root.CFrame * CFrame.new(0, 0, -7)
							hum.AutoRotate = true
							hum:MoveTo(c.Position)
						end

						if not timeout then
							hum.Jump = true
							policemodule.walktotarget(nearesttarget)
							break
						end
					end
				else
					--[[local known = game.ReplicatedStorage.CommsKnownPosition
					if known.Value ~= Vector3.new(0, 0, 0) then
						comms(nearesttarget)]]--
					else
						wander(hum, root)
					end
				end
			end
		else
			--[[local known = game.ReplicatedStorage.CommsKnownPosition
			known.Value = Vector3.new(0, 0, 0)]]--
		end 
	else
		wander(hum, root)
	end
end

function raycastsight(nearest, character)

	if nearest then
		
		local direction = (nearest.Position - character.Head.Position).Unit * 300

		local raycastparams = RaycastParams.new(
			character.Head,
			character.Torso,
			character["Right Arm"],
			character["Left Arm"],
			character["Right Leg"],
			character["Left Leg"]
		)
		raycastparams.FilterType = Enum.RaycastFilterType.Exclude

		local raycast = workspace:Raycast(character.Head.Position, direction, raycastparams)

		if raycast then
			if raycast.Instance.Parent:FindFirstChild("Humanoid") and raycast.Instance.Parent:FindFirstChild("HumanoidRootPart") and raycast.Instance.Parent:FindFirstChild("Type").Value == "Criminal" then
				local known = game.ReplicatedStorage.CommsKnownPosition
				known.Value = raycast.Position
				shoot(character.HumanoidRootPart, raycast.Position)
				return true
			else
				return false
			end
		end
	end
end

function shoot(root, placetoshoot)

	if placetoshoot then
		bullet(root.Position, placetoshoot)
		task.wait(0.3)
	end
end

function bullet(startingpos, direction)

	local bullet = game.ReplicatedStorage.Bullet:Clone()
	bullet.CFrame = CFrame.new(startingpos, direction)
	bullet.Parent = workspace

	game:GetService("Debris"):AddItem(bullet, 5)
end

return policemodule

local module = game.ReplicatedStorage.ModuleScript
local req = require(module)

while true do
	task.wait(1)
	req.walktotarget()
end

sorry if this is topic is kinda unclear, also my coding is horrible and im trying to get better. pls criticize me, but also help me find out the coding error :+1: :+1:

You have it set up so walktotarget expects 3 inputs, but are providing it with 0.
Please adjust where you call the function so that the expected parameters are provided

1 Like

Missing root argument.

(This extra char ignore it)

1 Like

i am so sorry i forgot to do these things lemme do the things yall are asking me to do rq

it works thank you ppl :sob: :folded_hands:
(chars minimum sorry)

1 Like

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