Hello! So, i’m making a game (mostly AI controlled) to increase my understanding on AI.
So, you may ask… Whats the problem? It works fine, but I want multiple foodstores and it walks to the closest one. Example: The dummy gets hungry and hes within 100 studs of a foodstore, he goes in and his hunger is replenished. The dummy walks away, and later hes within 100 studs of a DIFFERENT foodstore, he goes to that instead of the one he had just went to.
Hope that makes sense.
local dummy = script.Parent.Dummy.Humanoid
local magnitude = (script.Parent.Position-workspace.World.FoodStore.Position).magnitude
if magnitude <= 100 then
dummy:MoveTo(workspace.World.FoodStore.Position)
end
Get variables which you can use for: hunger, closestFoodStore (or foodStore if you only want one food store in your city or something)
then for every second (or 2 or 3 sec idk), you can subtract the hunger to something small and you will need to constantly check if the hunger is == 0 or <value and after checking it, make it so that it uses pathfinding service so that the NPC finds it way to a nearest foodstore
(if you have multiple foodstores, make a for loop in the foodstore folder checking for a part and its magnitude with that npc and set the closestFoodstore variable to the nearest foodstore checked)
and after pathfinding, use the .Wat() function to check if the NPC is in the foodstore and make it so the hunger restores to max or a specific value you want.
Make sure it’s in a loop or else it will only play once
Store the destinations in a folder and loop through it when the npc becomes famished.
for _, store in ipairs(stores:GetChildren()) do
local primary = store.PrimaryPart
local hrp = character.HumanoidRootPart
local distance = (primary.Position - hrp.Position).Magnitude
if distance < range then
-- use pathfind service instead of basic MoveTo
end
end