NPCs don't move out of way for Bikes

I’m currently working on a civilian system for my game which has bikes but the NPCs don’t move out of the way if a bike is close to them, I have a pathfinding modifier around the bike and the cost set to math.huge but they still don’t try to move out of the way.

script:

local PathfindingService = game:GetService("PathfindingService")
local ServerStorage = game:GetService("ServerStorage")

local SimplePath = require(ServerStorage.SimplePath)

local function npcGun(tool, target, npc)
	while npc.Parent and npc.Humanoid.Health > 0 do
		tool.Handle.Server.NPCShoot:Fire(target, npc)
		local randomTime = math.random(1, 3)
		if tool.Name == "Inf" then
			randomTime = 0.5
		end
		task.wait(randomTime)
	end
end

local function moveDummy(dummy)
	local attackingPlr = false
	local Goal = script.Parent.Waypoints[math.random(1, #script.Parent.Waypoints:GetChildren())]
	local e = {
		AgentRadius = 2,
		AgentHeight = 4,
		AgentCanJump = true,
		AgentCanClimb = true,
		Costs = {
			Player = 200,
			NPC = 200,
			Gun = math.huge,
			PassThrough = 1,
			DontPassThrough = 500,
			DontPassThroughAlt = math.huge,
			Vehicle = math.huge
		}
	}
	local Path = SimplePath.new(dummy, e)

	local blockedConnection
	blockedConnection = Path.Blocked:Connect(function()
		Path:Run(Goal)
	end)

	local errorConnection
	errorConnection = Path.Error:Connect(function(errorType)
		Path:Run(Goal)
	end)

	local reachedConnection
	reachedConnection = Path.Reached:Connect(function()
		if not attackingPlr then
			game.Debris:AddItem(dummy, math.random(30, 500))
		else
			Path:Run(Goal)
		end
	end)

	Path:Run(Goal)

	dummy.StopMoving.Event:Connect(function(newPath)
		if dummy.Humanoid.Health > 0 then
			print("Stopping path")
			Path:Stop()
			attackingPlr = true
				
			local items = ServerStorage.Tools:GetChildren()
			local randomItem-- = items[math.random(1, #items)]
			
			if dummy.Name == "King Von" then
				randomItem = ServerStorage.Tools.Inf
			else
				repeat
					task.wait(0)
					randomItem = items[math.random(1, #items)]
				until randomItem ~= ServerStorage.Tools.Inf
			end
			
			local tool = randomItem:Clone() --ServerStorage.Tools.Gun:Clone()
			tool.Parent = dummy
			dummy.Humanoid:EquipTool(tool)

			coroutine.wrap(function()
				npcGun(tool, newPath, dummy)
			end)()

			Goal = newPath:IsA("Player") and newPath.Character.HumanoidRootPart or newPath.HumanoidRootPart
			Path:Run(Goal)
		end
	end)
end

function getRandomNPC()
	local playerList = workspace.NPCS:GetChildren()
	for _, npc in ipairs(playerList) do
		if npc.Name == "NPC" then
			return npc
		end
	end
	return nil
end

local function spawnDummy()
	local items = ServerStorage.NPCs:GetChildren()
	local newDummy = items[math.random(1, #items)]:Clone()
	local DummySpawn = script.Parent.Spawns[math.random(1, #script.Parent.Spawns:GetChildren())]
	newDummy.Parent = workspace.NPCS
	newDummy.HumanoidRootPart.CFrame = DummySpawn.CFrame
	
	local NPM = ServerStorage.NPCPathfindingModifier:Clone()
	NPM.Parent = newDummy
	NPM.CFrame = newDummy.HumanoidRootPart.CFrame

	local NPMWeld = Instance.new("WeldConstraint")
	NPMWeld.Part0 = newDummy.HumanoidRootPart
	NPMWeld.Part1 = NPM
	NPMWeld.Parent = newDummy.HumanoidRootPart
	
	if newDummy.Name == "King Von" then
		local ableToAttack = script.Attacker:Clone()
		ableToAttack.Parent = newDummy
		ableToAttack.Enabled = true
		
		task.wait(1)
		
		if math.random(1, 2) == 1 then
			local randomNPC = getRandomNPC()
			if randomNPC then
				ableToAttack.RandomAttack:Fire(randomNPC)
			end
		else
			local playerList = game.Players:GetPlayers()
			if #playerList > 0 then
				local randomPlayer = playerList[math.random(1, #playerList)]
				if randomPlayer then
					ableToAttack.RandomAttack:Fire(randomPlayer)
				end
			end
		end
	else
		if math.random(1, 10) == 1 then
			local ableToAttack = script.Attacker:Clone()
			ableToAttack.Parent = newDummy
			ableToAttack.Enabled = true

			task.wait(1)
			if math.random(1, 2) == 1 then
				local randomNPC = getRandomNPC()
				if randomNPC then
					ableToAttack.RandomAttack:Fire(randomNPC)
				end
			else
				local playerList = game.Players:GetPlayers()
				if #playerList > 0 then
					local randomPlayer = playerList[math.random(1, #playerList)]
					if randomPlayer then
						ableToAttack.RandomAttack:Fire(randomPlayer)
					end
				end
			end
		end
	end
	
	coroutine.wrap(function()
		moveDummy(newDummy)
	end)()
end

while true do
	task.wait(math.random(0.25, 1))
	spawnDummy()
end

Pathfinding isn’t dynamic, Once you calculate a path its not gonna detect the bike if its not already in the way. the solution would be to re-calculate the path frequently

4 Likes

What part of the script would I change to make it recalculate frequently? Should I just have another coroutine.wrap for it?

I’m unsure as to what SimplePath is, but youd have to go to the move dummy function

Simplepath is a module to make pathfinding easy SimplePath - Pathfinding Module

The point is that, after you the PathfindingService do a calculation, for him, everything is paralyzed and nothing is moving, thats why the NPC dont move out of the way, because for the NPC, the bike isnt there!

To solve this, you need to do a new path, u can do this by making a loop, or by checking if a part is near the NPC, if theres a part (in this case the bike), the npc needs to do a new path

Hope it helps!

1 Like

I appreciate your help, but I’m a little confused about what you’re trying to say. Could you clarify or break it down a bit more for me?