All I want/need is my AI to track the player, I don’t know if its a scripting issue (I believe it is not) but rather I believe it is a issue with the server or something. I am using the SimplePath module for the pathfinding service so it could be related to that.
I am in dire need of help, I kinda need to fix this by tonight so if anyone knows how to help please help.
the following two clips show the issue, on the baseplate the AI is still delayed with tracking but not super delayed. On the map it is so delayed it is basically unusable. It might be an issue with the NavMesh, but I already checked and it looked fine.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Module = require(ReplicatedStorage.SimplePath)
local Players = game:GetService("Players")
local AI = script.Parent
local Target = nil
local Path = Module.new(AI)
local MaxDistance = math.huge
local Track = AI.Humanoid.Animator:LoadAnimation(script.Attack)
local ScreamTrack = AI.Humanoid.Animator:LoadAnimation(script.Scream)
local TimeDB = 0.4
local AttackDB = false
local Damage = 75
local Raging = false
Path.Visualize = true
local function playSound(sound : Sound, SoundPart)
local Clone = sound:Clone()
Clone.Parent = SoundPart
Clone:Play()
game:GetService("Debris"):AddItem(Clone, Clone.TimeLength)
end
Path.Blocked:Connect(function()
if Target ~= nil then
Path:Run(Target.HumanoidRootPart)
end
end)
Path.WaypointReached:Connect(function()
if Target ~= nil then
Path:Run(Target.HumanoidRootPart)
end
end)
Path.Error:Connect(function(errorType)
if Target ~= nil then
Path:Run(Target.HumanoidRootPart)
end
end)
local function SearchForClosestTarget()
local nearestTarget
local MaxDistance = MaxDistance
for index, player in pairs(Players:GetPlayers()) do
if player.Character then
local target = player.Character
local distance = (AI.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if distance < MaxDistance then
nearestTarget = target
MaxDistance = distance
end
end
end
return nearestTarget
end
local function IfCloseEnough(Target)
if Target:FindFirstChild("HumanoidRootPart") then
local distance = (AI.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude
if distance < 4 then
if AttackDB == false then
AttackDB = true
local Humanoid = Target.Humanoid
Track:Play()
Humanoid:TakeDamage(Damage)
wait(TimeDB)
AttackDB = false
end
end
end
end
local function visualizeRaycast(origin, destination)
local Part = Instance.new("Part")
Part.Color = Color3.fromRGB(138, 255, 65)
Part.CanCollide = false
Part.Anchored = true
Part.Size = Vector3.new(0.2, 0.2, (destination - origin).Magnitude + 1)
Part.CFrame = CFrame.lookAt((origin + destination) / 2, destination)
Part.Parent = workspace
game:GetService("Debris"):AddItem(Part, 5)
end
local function checkIfRage(Target)
local Origin = AI.Head.Position
local Direction = (Target.Head.Position - Origin).Unit * 500
local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Exclude
local Raycast = workspace:Raycast(Origin, Direction, RayParams)
if Raycast then
if Players:GetPlayerFromCharacter(Raycast.Instance.Parent) ~= nil or Players:GetPlayerFromCharacter(Raycast.Instance.Parent.Parent) ~= nil then
print("raging")
Raging = true
AI.HumanoidRootPart.Anchored = true
ScreamTrack:Play()
playSound(script.Sounds[math.random(1, #script.Sounds:GetChildren())], AI.Head)
ReplicatedStorage.Events.ScreenShake:FireAllClients(true)
ScreamTrack.Stopped:Wait()
ReplicatedStorage.Events.ScreenShake:FireAllClients(false)
AI.HumanoidRootPart.Anchored = false
AI.Humanoid.WalkSpeed += 5
end
end
end
while wait(0.001) do
Target = SearchForClosestTarget()
if Target ~= nil then
if Target:FindFirstChild("HumanoidRootPart") then
Path:Run(Target.HumanoidRootPart)
if Raging == false then
checkIfRage(Target)
end
IfCloseEnough(Target)
end
end
end
clip 1
clip 2