There is this game I have been making called Monster Tycoon and I have recently added a new monster variant, the Warrior Monster. However, when grouping it as a model either facing forwards or backwards along the Z axis, it always chases me backwards. It also doesn’t really chase me properly, when other monsters using identical scripts do. I will post the script for reference:
print("ChaseScript loaded")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local PathfindingService = game:GetService("PathfindingService")
local ModeChangeEvent = game.ReplicatedStorage:WaitForChild("SendOutModeChangeToServerEvent")
local npc = script.Parent
local npcHumanoid = npc.Humanoid
local npcHRP = npc.HumanoidRootPart
local path = nil
local waypoints = nil
local RANGE = 50
local mode
local touchDebounce = {}
local debounce = false
ModeChangeEvent.OnServerEvent:Connect(function(plr, m)
mode = m
end)
local function CreatePlayerPath(target)
local playerPath = PathfindingService:CreatePath({
AgentCanJump = true,
})
local targetHRP = target.Character:WaitForChild("HumanoidRootPart")
local success, errorMsg = pcall(function()
playerPath:ComputeAsync(npcHRP.Position, targetHRP.Position)
end)
if not success or playerPath.Status ~= Enum.PathStatus.Success then
--print("not success")
return nil
end
return playerPath
end
local function GetClosestPlayer()
local magnitudes = {}
local target = nil
for i,player in pairs(Players:GetPlayers()) do
local dist = player:DistanceFromCharacter(npcHRP.Position)
if dist <= RANGE then
table.insert(magnitudes, {dist, player})
end
end
table.sort(magnitudes, function(a, b)
return a[1] - b[1]
end)
if #magnitudes > 0 then
target = magnitudes[1][2]
end
--print("TARGET: ", target)
return target
end
local function onTouch(hit)
local char = hit.Parent
if char and char:IsA("Model") then
local hum = char:FindFirstChild("Humanoid")
if hum and char.Name ~= "Monster" and char.Name ~= "TemperedMonster" then
local player = Players:GetPlayerFromCharacter(char)
if player then
if not touchDebounce[char] then
print("Gotcha")
hum.Health -= 20
print(player.Name .. " has been hit")
touchDebounce[char] = true
task.spawn(function()
task.wait(1)
touchDebounce[char] = nil
end)
end
end
end
end
end
local targets
local target
for i, part in npc:GetChildren() do
--print(part.Name)
if part:IsA("BasePart") then
part.Touched:Connect(onTouch)
end
end
while true do
--local target = GetClosestPlayer()
if npc:GetAttribute("PlayerToBeChased") then
target = Players:FindFirstChild(npc:GetAttribute("PlayerToBeChased"))
else
--print("aint have an attribute like that lad")
end
if target and mode == "Fight" then
--print('targeet')
repeat task.wait() until target.Character or target.CharacterAdded:Wait()
print("Done")
--print(target)
path = CreatePlayerPath(target)
if path then
--print("path exists")
waypoints = path:GetWaypoints()
--print(waypoints)
for i,waypoint in pairs(waypoints) do
--print(waypoint.Position)
local part = Instance.new("Part")
part.Position = waypoint.Position
part.Size = Vector3.new(0.5, 0.5, 0.5)
part.Color = Color3.new(0.792157, 0, 0.0117647)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
game.Debris:AddItem(part, 0.5)
npcHumanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
npcHumanoid.Jump = true
end
end
end
--print("done")
else
npcHumanoid:MoveTo(npcHRP.Position)
path = nil
waypoints = nil
end
npcHumanoid.Died:Connect(function()
if npc:GetAttribute("PlayerToBeChased") then
print("my enemy")
local plr = Players:WaitForChild(npc:GetAttribute("PlayerToBeChased"))
local currentEnemies = 0
if plr:GetAttribute("Enemies") then
currentEnemies = plr:GetAttribute("Enemies")
end
plr:SetAttribute("Enemies", currentEnemies - 1)
print("another enemy")
end
npc:Destroy()
return
end)
if RunService and RunService.Heartbeat then
RunService.Heartbeat:Wait()
else
task.wait(0.1)
end
end