Hey guys! I'm trying to make ai for all monsters in my game. I code the first stage. They are spawning in certain area and they are individually and randomly moving in that area. There is no problem with idle mode. The problem is when someone attack them they should stop what they are doing and attack the player. When they are idle they are waiting random amount of time. I want the cancel that wait or idle movement when they are under attack and if the attacker runs away they should back idle mode. I don't have enought experience with lua and i can't build the logic. What should i use to make it work ? Is there any topic should i research ? Thanks!
This spawn script is child of region part :
--Define Storage service
local storage = game:GetService("ReplicatedStorage")
--Which mob should spawn?
local mob = "Zombie"
--Count of mobs
local mobCount = 7
--Randomly pick between these times. It will wait idle xx seconds.
local idleTime = {10,15}
--Coordinates for free walking region
local topLeft = Vector3.new((script.Parent.Position.X + (script.Parent.Size.X/2)),0,(script.Parent.Position.Z + (script.Parent.Size.Z/2)))
local bottomRight = Vector3.new((script.Parent.Position.X - (script.Parent.Size.X/2)),0,(script.Parent.Position.Z - (script.Parent.Size.Z/2)))
--Spawn all mobs
local randCoordinate = Random.new()
for i=1, mobCount do
local randomX = randCoordinate:NextInteger(topLeft.X,bottomRight.X)
local randomZ = randCoordinate:NextInteger(topLeft.Z,bottomRight.Z)
local spawned = storage[mob]:clone()
spawned.safeRegionX.Value = tostring(topLeft.X)..":".. tostring(bottomRight.X)
spawned.safeRegionZ.Value = tostring(topLeft.Z)..":".. tostring(bottomRight.Z)
spawned.minIdle.Value = idleTime[1]
spawned.maxIdle.Value = idleTime[2]
spawned.HumanoidRootPart.Position = Vector3.new(randomX,3,randomZ)
spawned.Parent = workspace.Mobs
end
And this script is child of zombie. Each zombie have it's unique script.
--Bool variable for check if there is any attacker
local attacker = false
--Random for idle wait time
local randIdle = Random.new()
--Fixed height for certain mob
local height = script.Parent.HumanoidRootPart.Position.Y
--Safe region for walk
local firstX = string.split(script.Parent.safeRegionX.Value,":")[1]
local secondX = string.split(script.Parent.safeRegionX.Value,":")[2]
local firstZ = string.split(script.Parent.safeRegionZ.Value,":")[1]
local secondZ = string.split(script.Parent.safeRegionZ.Value,":")[2]
while script.Parent.Humanoid.Health > 0 do
if attacker then
print("there is attacker")
else
print("No attacker!")
local moveX = randIdle:NextNumber(firstX,secondX)
local moveZ = randIdle:NextNumber(firstZ,secondZ)
script.Parent.Humanoid:MoveTo(Vector3.new(moveX,height,moveZ))
print("Moving to : " .. tostring(moveX) .. ", " .. tostring(height) .. ", " .. tostring(moveZ))
wait(randIdle:NextInteger(script.Parent.minIdle.Value,script.Parent.maxIdle.Value))
end
end

2 comments
can u show us all scripts plox
I add my script