Hello everyone, this is going to be a long one. I am making an AI, no further explanation needed. I decided to make a hunt mode in the AI, see the hunt mode function and the loop at the bottom. However, for this FIRST and only FIRST round of hunting, the AI just does not path find to the random room. Also, the animation just starts playing as soon as it starts walking, and there is clearly a MoveToFinished:Wait() set up in the MovetO() function. The thing is, The MoveTo() function is not the issue because it works perfectly fine in the idle mode. If you need a video, please tell me, IM DESPERATE
-- Services
local pathfindingservice = game:GetService("PathfindingService")
local replstor = game:GetService("ReplicatedStorage")
local soundserv = game:GetService("SoundService")
-- Modules
local idletasks = require(script.IdleTasks)
-- Neighbor
local neighbor = script.Parent
local humanoid = neighbor:WaitForChild("Humanoid")
local hrp = neighbor:WaitForChild("HumanoidRootPart")
local head = neighbor:WaitForChild("Head")
hrp:SetNetworkOwner(nil)
-- Attributes
neighbor:SetAttribute("Mode", "Idle")
neighbor:SetAttribute("CanSeePlayer", false)
local mode = neighbor:GetAttribute("Mode")
local canseePlayer = neighbor:GetAttribute("CanSeePlayer")
--Workspace
local rooms = workspace:WaitForChild("Rooms"):GetChildren()
local spawns = workspace:WaitForChild("NeighborSpawns"):GetChildren()
local distractions = workspace:WaitForChild("Distractions")
--Bindables and Remotes
local bindables = replstor:WaitForChild("Bindables")
local remotes = replstor:WaitForChild("RemoteEvents")
local distractevent = bindables:WaitForChild("Distraction")
local plyrCaughtB = bindables:WaitForChild("playerCaught")
local plyrCaughtR = remotes:WaitForChild("playerCaught")
-- Nils
local lastknownPOS
local currentDistraction
-- Sounds
local catchsounds = soundserv.Effects.Catch:GetChildren()
-- Tables
local prevRooms = {}
-- Booleans
local alreadyDistracted = false
for i, v in pairs (rooms) do
v:SetAttribute("Favorite", false)
end
local function getRandomRoom()
for i = 1, #rooms do
local randomRoom = rooms[math.random(1, #rooms)]
if table.find(prevRooms,randomRoom) ~= nil then
repeat
randomRoom = rooms[math.random(1, #rooms)]
task.wait()
until table.find(prevRooms,randomRoom) == nil
end
table.insert(prevRooms,randomRoom)
task.wait()
return randomRoom
end
end
local function catch(intruder) -- Catch the Intruder
if intruder then
local anim = humanoid.Animator:LoadAnimation(script.Animations.Catch)
local random = catchsounds[math.random(1, # catchsounds)]
anim:Play()
random:Play()
plyrCaughtR:FireClient(game.Players:GetPlayerFromCharacter(intruder))
humanoid.WalkSpeed = 0
humanoid.JumpHeight = 0
intruder.Humanoid.WalkSpeed = 0
intruder.Humanoid.JumpHeight = 0
wait(2)
intruder.HumanoidRootPart.Position = workspace.Spawn.Position
intruder.Humanoid.WalkSpeed = 16
intruder.Humanoid.JumpHeight = 7.4
plyrCaughtB:Fire()
end
end
local function findIntruder() -- Finding the intruder, can see
local players = game.Players:GetPlayers()
local intruder
for i, player in pairs (players) do
local char = player.Character or player.CharacterAdded:Wait()
if char then
local distance = (char.HumanoidRootPart.Position - hrp.Position).Magnitude
if distance < 50 then
local direction = (char.HumanoidRootPart.Position - hrp.Position).Unit * 50
local params = RaycastParams.new(neighbor)
params.FilterType = Enum.RaycastFilterType.Exclude
local rayResult = workspace:Raycast(hrp.Position, direction, params)
if rayResult then
if rayResult.Instance:IsDescendantOf(char) then
intruder = char
end
end
end
end
end
return intruder
end
local function getPath(finish) -- Creating Paths
local path = pathfindingservice:CreatePath({
AgentRadius = 1,
AgentHeight = 6,
AgentCanJump = false,
AgentCanClimb = false,
WaypointSpacing = 2
})
path:ComputeAsync(hrp.Position, finish)
return path
end
local function moveTo(finish) -- Moving To
local path = getPath(finish)
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs (path:GetWaypoints()) do
local intruder = findIntruder()
if intruder then
mode = "Attack"
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(finish - (hrp.CFrame.LookVector * 10))
end
end
local function attack(intruder) -- Attack
if intruder then
lastknownPOS = nil
local distance = (hrp.Position - intruder.HumanoidRootPart.Position).Magnitude
if distance > 2 then
humanoid:MoveTo(intruder.HumanoidRootPart.Position)
lastknownPOS = intruder.HumanoidRootPart.Position
elseif distance < 2.5 then
catch(intruder)
end
else
if lastknownPOS then
moveTo(lastknownPOS)
lastknownPOS = nil
elseif not lastknownPOS then
mode = "Idle"
end
end
end
local function idle() -- Idle Mode
local room = rooms[math.random(1, #rooms)]
moveTo(room.Label.Position)
end
local function hunt() -- Hunting
local randomroom = getRandomRoom()
local anim = humanoid.Animator:LoadAnimation(script.Animations.Look)
moveTo(randomroom.Label.Position)
if mode ~= "Attack" then
anim:Play()
head.Sounds.Grunt:Play()
anim.Stopped:Wait()
end
end
local function changeMode()
for i, instance in pairs (distractions:GetDescendants()) do
if instance:IsA("BoolValue") and instance.Name == "Status" then
if instance.Value == true and mode.Value ~= "Attack" then
local lookAnim = script:WaitForChild("Animations"):WaitForChild("Look")
moveTo(instance.Parent.Screen.Position)
if mode ~= "Hunt" and mode ~= "Attack" then
mode = "Hunt"
instance.Value = false
local anim = humanoid.Animator:LoadAnimation(script.Animations.Look)
anim:Play()
neighbor.Head.Sounds.Yell2:Play()
anim.Stopped:Wait()
end
end
end
end
end
while task.wait(.1) do -- Loop
changeMode()
print(mode)
if mode == "Idle" then
idle()
elseif mode == "Hunt" then
repeat
hunt()
until #prevRooms == #rooms
table.clear(prevRooms)
mode = "Idle"
elseif mode == "Attack" then
attack(findIntruder())
end
end