Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I fix this AI standing still when it shouldnt? Real All

So in my game, I have AI workers similar to Retail Tycoon. So I have this AI module that uses pathfinding service to go to the waypoints it sets. So i found out that the character tries to go to other waypoints they didn't even reach yet which makes them go into walls. So i made a Magnitude check to make sure if the ai is close the waypoint then go to the next, but if another AI moves the original AI in any other way it just stand still. How would i tackle this issue?

local module = {}
	function module.RunAI(Goal,Character)
		math.randomseed(tick())
		math.random()
		local pfs = game:GetService("PathfindingService")
		local Waypoints = Goal:GetChildren()
		while true do 
			local Waypoint = Waypoints[math.random(1,#Waypoints)] 
			local FoundPath = pfs:FindPathAsync(Character.HumanoidRootPart.CFrame.p,Waypoint.CFrame.p)
			local Path = FoundPath:GetWaypoints()
			for i = 1,#Path do 
				if Path[i].Action == Enum.PathWaypointAction.Jump then
					Character.Humanoid.Jump = true
				else
					repeat wait() until Path[i].Position.Magnitude - Character.HumanoidRootPart.CFrame.p.Magnitude < 4
					Character.Humanoid:MoveTo(Path[i].Position)
				end
			end
			wait(math.random(2,8))
		end
	end
return module

6 comments

0

When you use . Instead of : , the first parameter is always the table/object.

0

On what line your talking about @incapz

0

line 2, also i don't think so?

0

That isnt the issue re read the questioni asked.

0

why are you making it go to a random waypoint each time? (unrelated, but just curious)

0

i want the character to go to its work place sit then get up to get papers or to get some drinks. Make it look dynamic

1 answer

I believe the culprit is line 15

repeat wait() until Path[i].Position.Magnitude - Character.HumanoidRootPart.CFrame.p.Magnitude < 4

This causes the AI to keep on waiting, and waiting. I think you meant to put the line under it in front of it?

This discussion is preserved as it was. New answers, comments, and votes are closed.