I’m making a decaying winter npc testing type of game and I’ve been having this bug for a while. So I use magnitude to calculate how far an npc can hit but every few minutes the npc begins to lag a bit then the magnitude calculation will break, and will cause the npc not to melee in the same range as it was before the problem starts happening.
Npc Module Shortened
local PathfindModule = require(script.Parent.NPC_PathFind)
local CombatModule = require(script.Parent.NPC_Combat)
local NPC = {}
local effectModule = require(game.ReplicatedStorage.gunstats.EffectStats)
NPC.__index = NPC
local events = game.ReplicatedStorage.Events
local cosmetics = game.ReplicatedStorage.Cosmetics
local Debris = game:GetService("Debris")
local C_WAIT = function(dt)
dt = dt or 1/60
local now = tick()
repeat
game:GetService('RunService').Heartbeat:Wait()
until tick() - now >= dt
end
local MAIN = require(script.Parent.Main)
NPC.CURRENTNPCS = {}
NPC.Faction = 2
local Dialogues = require(script.Parent.NPC_Dialogue)
function NPC:CreateNPC(NPCMODEL : Model,Time : number,Range : number,turnspeed : number,faction:number,speed: number,attackplrs)
local self = setmetatable({},NPC)
self.NPC = NPCMODEL
print(NPC.CURRENTNPCS)
self.ThirdMain = {}
self.MoveTime = Time
if not speed then
self.speed = 1
else
self.speed = speed
end
self.Range = Range
self.Turned = false
self.Targets = {}
self.Target = nil
self.CANSEE = false
self.Crouching = false
self.Pathing = false
self.Tick = tick()
self.CurrentAttack = nil
if self.NPC:FindFirstChild("AttackPlayers") then
self.AttackPlayers = true
else
self.AttackPlayers = false
end
self.Warning = 0
self.Path = game:GetService("PathfindingService"):CreatePath(PathfindModule.ReturnPathParams())
self.Root = self.NPC.PrimaryPart
self.Head = self.NPC.Head
self.OGHEADC0 = self.NPC.Torso.Neck.C0
self.Ignore = {self.NPC,workspace.Terrain}
self.Weapon = nil
self.NPCGOAL = nil
self.pathFailCount = 0
self.Ping =0
self.Faction = 1
self.Staggered = false
self.Priority = nil
self.Origin = CFrame.new(self.Root.Position)
self.Walkspeed = 13
self.LoudFire = false
self.Damaged = false
self.TargetHead = nil
if self.NPC:FindFirstChild("ChaseSong") then
self.UsesChaseSong = true
self.ChaseSong = self.NPC:FindFirstChild("ChaseSong").Value
else
self.UsesChaseSong = false
end
self.SPRINTSPEED = 21
self.MOVEMENTDEBOUNCE = true
self.COVERING = false
self.COMBATDEBOUNCE = true
self.REACTIONTIME = 0.475/self.speed
self.Covertime = 25
print("created "..NPCMODEL.Name.." into the NpcModule")
NPC.CURRENTNPCS[NPCMODEL] = self
if self.NPC.PrimaryPart ~= nil then
local character = NPCMODEL
local Alignment
local charactervalue = game.ReplicatedStorage.Client.CharacterValues:Clone()
self.CharacterValues = charactervalue
self.LOOK = Instance.new("IKControl")
game.CollectionService:AddTag(self.NPC,'NPC')
self.CurrentHp = self.NPC.Humanoid.Health
self.CharacterValues.States.Stagger:GetPropertyChangedSignal("Value"):Connect(function(value)
if self.CharacterValues.States.Stagger.Value == true then
self.Staggered = true
self.COMBATDEBOUNCE = false
print("test2")
task.wait(2.3)
self.Staggered = false
if self.COMBATDEBOUNCE then return end
self.COMBATDEBOUNCE = true
end
print("test")
end)
task.spawn(function()
local test = game.ReplicatedStorage.Client["Test"]:Clone()
test.Parent = character
test.Enabled = true
end)
self.NPC.Humanoid.HealthChanged:Connect(function(old)
if self.CurrentHp > old then
print("Damaged")
task.spawn(function()
while self.CharacterValues.States.Stagger.Value do
self.COMBATDEBOUNCE = false
self.Staggered = true
self.MOVEMENTDEBOUNCE = false
self.NPC.Humanoid.WalkSpeed = 0.001
task.wait()
if not self.CharacterValues.States.Stagger.Value then
task.wait(2.3)
self.Staggered = false
if self.COMBATDEBOUNCE then return end
self.COMBATDEBOUNCE = true
end
end
end)
self.Damaged = true
task.spawn(function()
task.wait(5)
if not self.CANSEE then
self.Damaged = false
end
end)
end
end)
charactervalue.Parent = character
for i,v in pairs(effectModule) do
print(i)
local effectvalue = game.ReplicatedStorage.Client.EffectPlaceholder:Clone()
effectvalue.Name = i
effectvalue.Parent = character.CharacterValues.Effects
end
--
self.NPC.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
self.NPC.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
self.NPC.Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
if not character:FindFirstChild("Animate") then
local animate = game.ReplicatedStorage.Client.Animate:Clone()
animate.Parent = character
animate.Enabled = true
end
self.LOOK.Type = Enum.IKControlType.LookAt
self.LOOK.ChainRoot = self.Head
self.LOOK.EndEffector = self.Head
self.LOOK.Parent = self.NPC.Humanoid
self.LOOK.Enabled = true
self.LOOK.SmoothTime = 0.35
self.LOOK.Weight = 0
if not self.NPC:FindFirstChild("AlignOrientation",true) then
Alignment = Instance.new("AlignOrientation")
Alignment.Enabled = false
Alignment.Parent = self.NPC.PrimaryPart
Alignment.Attachment0 = self.NPC.PrimaryPart.RootAttachment
Alignment.Mode = Enum.OrientationAlignmentMode.OneAttachment
Alignment.Responsiveness = 200
if turnspeed then
Alignment.MaxTorque = turnspeed
else
Alignment.MaxTorque = 2500
end
self.Alignment = Alignment
else
self.NPC:FindFirstChild("AlignOrientation",true):Destroy()
Alignment = Instance.new("AlignOrientation")
Alignment.Enabled = false
Alignment.Parent = self.NPC.PrimaryPart
Alignment.Attachment0 = self.NPC.PrimaryPart.RootAttachment
Alignment.Mode = Enum.OrientationAlignmentMode.OneAttachment
Alignment.Responsiveness = 200
if turnspeed then
Alignment.MaxTorque = turnspeed
else
Alignment.MaxTorque = 2500
end
self.Alignment = Alignment
end
if not self.NPC:FindFirstChild("faction") then
local value = Instance.new("NumberValue",self.NPC)
value.Name = "faction"
value.Value = 2
end
local characterClone = character:Clone()
if self.NPC:FindFirstChild("Boss") then
local bossHealth = game.ReplicatedStorage.UI.bossHealthBar:Clone()
bossHealth.Parent = self.NPC
bossHealth.Adornee = self.Root
bossHealth.HealthScript.Enabled = true
end
self.NPC.Humanoid.BreakJointsOnDeath = false
if self.NPC:FindFirstChild("Outfit") then
for _,v in pairs(self.NPC:FindFirstChild("Outfit"):GetDescendants()) do
if v:IsA("BasePart") then
v.CanCollide = false
v.CanQuery = false
v.Massless = true
print("Outfit")
end
end
end
self.NPC.Humanoid.Died:Connect(function()
self:stopanimations()
self.LOOK.Weight = 0
print("died")
if self.UsesChaseSong then
for i,v in pairs(game:GetService("Players"):GetPlayers()) do
if v:IsA("Player") then
events.EventsClient:FireClient(v,"spotted",false,self.ChaseSong)
end
end
end
Alignment:Destroy()
self:ON_KILLED(self.NPC)
self.ThirdMain["Gun death"].Priority = Enum.AnimationPriority.Action4
self.ThirdMain["Gun death"]:Play(0.1,1,1.5)
local SlotStats = require(game.ReplicatedStorage.gunstats.RangedWeaponStats)[self.Weapon]
task.spawn(function()
task.wait(self.ThirdMain["Gun death"].Length * 0.99)
self.ThirdMain["Gun death"]:AdjustSpeed(0)
end)
if not game.Players:GetPlayerFromCharacter(self.NPC) then
events.effect:FireAllClients("Ragdoll",character,self.ThirdMain["Gun death"].Length * 0.5)
end
self.NPC.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, true)
self.NPC.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
task.wait(0.2)
if self.NPC:FindFirstChild(self.Weapon) then
self.NPC:FindFirstChild(self.Weapon):Destroy()
local weapon = game.ReplicatedStorage.weapons:FindFirstChild(self.Weapon):Clone()
weapon.PrimaryPart = weapon.JointGrip
weapon.Parent = workspace.Exclusion
weapon:PivotTo(self.Root.CFrame)
weapon.JointGrip.CanCollide = true
end
for _,v in pairs(self.NPC:GetChildren()) do
if string.match(string.upper(v.Name),"NPC_") then
if v:IsA("Script") then
v.Enabled = false
end
end
end
task.wait(10)
self.NPC:Destroy()
end)
for _,v in pairs(self.NPC:GetDescendants()) do
if v:IsA("BasePart") then
v.Locked = false
if v.Name ~= "HumanoidRootPart" and v.Name ~= "Torso" and v.Name ~= "Head" then
v.Massless = true
end
end
end
end
return self
end
function NPC:ON_KILLED(character)
if not character then
return;
end
print("test")
if character:FindFirstChild('HasDied') then
return;
end
print("test")
if character:FindFirstChild('LastHit') then
local LastHit = character.LastHit
local Killer = LastHit.Value
local HasDied = Instance.new('BoolValue')
HasDied.Name = 'HasDied'
HasDied.Value = true
HasDied.Parent = character
print("test")
if Killer and Killer:IsA('Player') then
game.ReplicatedStorage.Events.Kill:FireClient(Killer,1)
Killer.Kills.Value += 1
if Killer.Perk.Value == "SQ" then
Killer.Character.Humanoid.Health += Killer.Character.Humanoid.MaxHealth/2
end
print("test")
end
return true;
else
return false;
end
end
function NPC:GiveWeaponToNpc(item,character)
local weapon = game.ReplicatedStorage.weapons
local SlotStats = require(game.ReplicatedStorage.gunstats.RangedWeaponStats)[item]
for i ,v in pairs(character:GetChildren()) do
if v:IsA("Model") and weapon:FindFirstChild(v.Name) then
v:Destroy()
end
end
self.Weapon = item
if weapon:FindFirstChild(item) then
local gun = weapon:FindFirstChild(item):Clone()
gun.Parent = character
local joint = Instance.new("Motor6D")
joint.Parent = character:FindFirstChild("Right Arm")
joint.Part0 = character:FindFirstChild("Right Arm")
joint.Part1 = gun.JointGrip
joint.C0 = CFrame.new(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
joint.Name = "W_JOINT"
local Value = CFrame.new(0, 0, 0)
for i ,v in pairs(gun:GetDescendants()) do
if v:IsA("BasePart") then
v.CanCollide = false
v.Locked = false
v.Massless = true
v.Anchored = false
end
end
if game.ReplicatedStorage.Animations.Thirdperson[SlotStats.AnimationType]:FindFirstChild('weldval') then
Value = game.ReplicatedStorage.Animations.Thirdperson[SlotStats.AnimationType].weldval.Value
end
joint.C1 = Value
end
return SlotStats
end
function NPC:GiveWeapon(item)
local weapon = game.ReplicatedStorage.weapons
local SlotStats = require(game.ReplicatedStorage.gunstats.RangedWeaponStats)[item]
self:GetAnims(item)
for i ,v in pairs(self.NPC:GetChildren()) do
if v:IsA("Model") and weapon:FindFirstChild(v.Name) then
v:Destroy()
end
end
if not SlotStats or not item then
item = "Fists"
self.Weapon = item
SlotStats = require(game.ReplicatedStorage.gunstats.RangedWeaponStats)[item]
elseif SlotStats.WeaponType == "Item" then
item = "Fists"
end
self.Weapon = item
if weapon:FindFirstChild(item) then
local gun = weapon:FindFirstChild(item):Clone()
gun.Parent = self.NPC
local joint = Instance.new("Motor6D")
joint.Parent = self.NPC:FindFirstChild("Right Arm")
joint.Part0 = self.NPC:FindFirstChild("Right Arm")
joint.Part1 = gun.JointGrip
joint.C0 = CFrame.new(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
joint.Name = "W_JOINT"
local Value = CFrame.new(0, 0, 0)
for i ,v in pairs(gun:GetDescendants()) do
if v:IsA("BasePart") then
v.CanCollide = false
v.Locked = false
v.Massless = true
v.Anchored = false
end
end
if game.ReplicatedStorage.Animations.Thirdperson[SlotStats.AnimationType]:FindFirstChild('weldval') then
Value = game.ReplicatedStorage.Animations.Thirdperson[SlotStats.AnimationType].weldval.Value
end
joint.C1 = Value
end
self:GetAnims(self:GetNPC().Weapon)
self:GetNPC().ThirdMain.Hold:Play(0.1)
return SlotStats
end
function NPC:Crouch()
local Crouchanim = self.ThirdMain.Crouch
Crouchanim:Play(0.1)
game:GetService("TweenService"):Create(self.NPC.Humanoid,TweenInfo.new(1),{HipHeight = -1}):Play()
task.wait(0.2)
game:GetService("TweenService"):Create(self.NPC.Humanoid,TweenInfo.new(1),{HipHeight = 0}):Play()
Crouchanim:Stop(0.1)
end
function NPC:Stand()
self.ThirdMain.Crouch:Stop()
self.ThirdMain.Crouch.Looped = false
wait(1)
self.Human.HipHeight = 0
end
function NPC:WalkToPosition(target)
if self.NPC.PrimaryPart == nil or self.Pathing == true then return end
task.spawn(function()
local Path = self.Path
local success, result = pcall(function()
self.Path:ComputeAsync(self.Root.Position,target)
end)
if not success then
return
end
local WayPoints = self.Path:GetWaypoints()
self.WayPoints = WayPoints
if Path and WayPoints then
for _, Point in pairs(WayPoints) do
local Reached = false
if Point.Action == Enum.PathWaypointAction.Jump then
self.NPC.Humanoid.Jump = true
end
if self.Pathing == true or self:CalculateDistanceFromVector(target) < 3 or self.Staggered then return end
self.Pathing = true
self.NPC.Humanoid:MoveTo(Point.Position)
self.NPC.Humanoid.MoveToFinished:Connect(function(Moved)
if Moved == false then
self.Pathing = false
task.wait(1)
end
Reached = true
end)
repeat task.wait() until Reached == true
self.Pathing = false
self.WayPoints = nil
end
end
end)
end
function NPC:GetNPC()
return self
end
function NPC:Deleteanimations()
if self.NPC.PrimaryPart == nil then return end
local Anims = self.NPC.Humanoid.Animator:GetPlayingAnimationTracks()
for _,animation in Anims do
animation:Destroy()
end
end
function NPC:stopanimations()
if self.NPC.PrimaryPart == nil then return end
local Anims = self.NPC.Humanoid.Animator:GetPlayingAnimationTracks()
for _,animation in Anims do
animation:Stop(2)
end
end
function NPC:LookAt(Character : Model,target : BasePart)
if self.NPC.PrimaryPart == nil and not target or not self:CheckTarget() or self.Staggered then return end
--
C_WAIT()
if self.CharacterValues.States.Stagger.Value then
Character.Humanoid.AutoRotate = true
self.Alignment.Enabled = false
return
end
Character.Humanoid.AutoRotate = false
self.Alignment.Enabled = true
local diff = target.Position - self.Root.Position
local angle = math.atan2(-diff.X, -diff.Z)
local AlignOrientation = self.Root:FindFirstChild("AlignOrientation")
if self.NPC.PrimaryPart == nil and not target or not self.Root:FindFirstChild("AlignOrientation") or not Character:FindFirstChild("Torso") then return end
AlignOrientation.CFrame = CFrame.Angles(0, angle, 0)
events.neck:FireAllClients(Character.Torso,math.asin((target.Position - Character.Torso.Position).Unit.Y))
end
function NPC:useitem()
end
function NPC:TurnHead(Character,target,bool)
if self.NPC.PrimaryPart == nil or Character:HasTag("Shadow")then return end
--
C_WAIT()
if bool then
self.Turned = true
self.LOOK.Target = target
self.LOOK.Enabled = true
self.LOOK.Weight = 1
self.Alignment.MaxTorque = 500
self:LookAt(self.NPC,target)
if math.random(1,100) == 3 and not self.AttackPlayers then
-- if not self.Crouching then
-- task.spawn(function()
--- self.Crouching = true
-- self:Crouch()
-- task.wait(0.2)
-- self:Crouch()
-- task.wait(20)
-- self.Crouching = false
-- end)
-- end
end
else
if self.Turned == true then
self.Turned = false
self.LOOK.Weight = 0
self.Alignment.MaxTorque = 2500
end
end
end
function NPC:CalculateDistance()
if self.Root and self.Target then
print((self.Root.Position - self.Target.Position).Magnitude)
return (self.Root.Position - self.Target.Position).Magnitude
end
end
function NPC:CalculateDistanceFromVector(Target)
if self.NPC.PrimaryPart == nil or not Target or Target == nil or not self.NPC.PrimaryPart or not self.Root or not self.NPC then
else
return (self.Root.Position - Target).Magnitude
end
end
function NPC:findTarget()
--
if not self.NPC or self.NPC.PrimaryPart == nil or not self.Root then return end
spawn(function()
local dist = self.Range
local target = nil
local targethead = nil
local potentialTargets = {}
local seeTargets = {}
for i,char in game.Players:GetPlayers() do
local v = char.Character if not v then return end
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
local Head = v:FindFirstChild("Head")
if human and torso and self.NPC then
local root = self.Root if not root then return end
if not root.Anchored and self.Target and self.Target.Parent == char then
root:SetNetworkOwner(char)
self.Ping = char:GetNetworkPing()
end
if self.NPC:FindFirstChild('LastHit') then
local LastHit = self.NPC.LastHit
if self.NPC:FindFirstChild('LastHit') and self.NPC:FindFirstChild('LastHit').Changed and self.Damaged and self.Warning < 4 and self.NPC:FindFirstChild('LastHit').Value == char and not self.AttackPlayers then
self.Damaged = false
self.Warning += 1
if self.Warning > 3 then
local ChatService = game:GetService("Chat")
local talkpart = self.Head
local text = Dialogues.Normal
if self.NPC:HasTag("twilight") then
ChatService:Chat(talkpart, "Ok bruh you're cooked", "Red")
else
ChatService:Chat(talkpart, "ENOUGH", "Red")
end
ChatService.Chatted:Connect(function()
MAIN.PlaySound("general","killfeed",NPC.HumanoidRootPart)
end)
task.wait(1)
else
local ChatService = game:GetService("Chat")
local talkpart = self.Head
if self.NPC:HasTag("twilight") then
local text = Dialogues.Twilight
ChatService:Chat(talkpart, text.Damaged[math.random(1,#text.Damaged)], "Red")
else
local text = Dialogues.Normal
ChatService:Chat(talkpart, text.Damaged[math.random(1,#text.Damaged)], "Red")
end
ChatService.Chatted:Connect(function()
MAIN.PlaySound("general","killfeed",NPC.HumanoidRootPart)
end)
end
print(self.Warning)
end
if LastHit.Value == char and (self.Warning > 3 or self.AttackPlayers) then
self.AttackPlayers = true
self.Priority = torso
target = torso
targethead = Head
end
end
if not self.Target then
self.Priority = nil
end
if (root.Position - torso.Position).Magnitude < 10 and human.Health > 0 and char ~= self.NPC and not self.NPC:HasTag("Shadow") then
local dot = self.Root.CFrame.LookVector:Dot((root.Position - torso.Position).Unit)
if dot < math.cos(math.rad(120)) then
if self.Turned == false then
self:TurnHead(self.NPC.Torso,Head,true)
end
else
if self.Turned == true then
self:TurnHead(self.NPC.Torso,torso,false)
end
end
else
if self.Turned == true then
self:TurnHead(self.NPC.Torso,torso,false)
end
end
if self.AttackPlayers == true then
if (root.Position - torso.Position).Magnitude < 500 and human.Health > 0 and not self.Targets[v] and game.Players:GetPlayerFromCharacter(v) and self.UsesChaseSong and self.Target == torso and self.CANSEE then
events.EventsClient:FireClient(game.Players:GetPlayerFromCharacter(v),"spotted",true,self.ChaseSong)
end
if (root.Position - torso.Position).Magnitude > 500 or self.NPC.Humanoid.Health <= 0 or not self or not self.NPC or human.Health <= 0 and game.Players:GetPlayerFromCharacter(v)then
if self.UsesChaseSong then
events.EventsClient:FireClient(game.Players:GetPlayerFromCharacter(v),"spotted",false,self.ChaseSong)
self.Priority = nil
end
end
if (root.Position - torso.Position).Magnitude < dist and human.Health > 0 and not self.Targets[v] then
target = torso
targethead = Head
dist = (root.Position - torso.Position).Magnitude
table.insert(potentialTargets,torso)
end
end
end
end
for i,char in NPC.CURRENTNPCS do
local v = char.NPC
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
local Head = v:FindFirstChild("Head")
if human and torso and self.NPC then
local root = self.Root if not root then return end
if not root.Anchored and self.Target and self.Target.Parent == char then
root:SetNetworkOwner(nil)
end
if self.NPC:FindFirstChild('LastHit') and (v:FindFirstChild("faction") and v:FindFirstChild("faction").Value == self.NPC:FindFirstChild("faction").Value ) and self.Warning > 3 then
char.AttackPlayers = true
char.Damaged = true
char.CANSEE = true
end
if not self.Priority then
if (root.Position - torso.Position).Magnitude < dist and human.Health > 0 and (v:FindFirstChild("faction") and v:FindFirstChild("faction").Value ~= self.NPC:FindFirstChild("faction").Value and not self.Targets[v]) and char.LoudFire then
target = torso
targethead = Head
dist = (root.Position - torso.Position).Magnitude
table.insert(potentialTargets,torso)
end
if (root.Position - torso.Position).Magnitude < dist and human.Health > 0 and (v:FindFirstChild("faction") and v:FindFirstChild("faction").Value ~= self.NPC:FindFirstChild("faction").Value and not self.Targets[v]) then
target = torso
targethead = Head
dist = (root.Position - torso.Position).Magnitude
table.insert(potentialTargets,torso)
end
if (root.Position - torso.Position).Magnitude < dist and human.Health > 0 and (v:FindFirstChild("faction") and v:FindFirstChild("faction").Value ~= self.NPC:FindFirstChild("faction").Value and not self.Targets[v]) and char.CANSEE and char.Target == self.Root and char.COMBATDEBOUNCE then
target = torso
targethead = Head
dist = (root.Position - torso.Position).Magnitude
table.insert(potentialTargets,torso)
end
if (root.Position - torso.Position).Magnitude < dist and human.Health > 0 and (v:FindFirstChild("faction") and v:FindFirstChild("faction").Value ~= self.NPC:FindFirstChild("faction").Value and not self.Targets[v]) and char.CANSEE and char.Target == self.Root then
target = torso
targethead = Head
dist = (root.Position - torso.Position).Magnitude
table.insert(potentialTargets,torso)
end
end
end
end
self.Targets = potentialTargets
if #potentialTargets > 3 and self.CANSEE then
self.COVERING = true
else
self.COVERING = false
end
if self.Target == target or self.TargetHead == targethead then return end
self.Target = target
self.TargetHead = targethead
task.wait(self.REACTIONTIME)
end)
end
local function findCover(hider, seeker, raycastParams)
local hiderRoot = hider
local seekerRoot = seeker
if not hiderRoot then
return
end
if not seekerRoot then
return
end
local radius = 20
local nPoints = 20
local maxRadiusSteps = 5
local radiusStep = 0
local coverPosition
repeat
radiusStep += 1
for i = 1, nPoints do
local angle = math.pi*2/nPoints * i
local cf = hiderRoot.CFrame * CFrame.Angles(0, angle, 0) * CFrame.new(0, 0, radius*radiusStep)
local origin = cf.Position
local direction = (seekerRoot.Position - origin)
if not raycastParams then
raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {hider.Parent, seeker.Parent}
end
local raycastResult = workspace:Raycast(origin, direction, raycastParams)
if raycastResult then
coverPosition = origin
break
end
end
until coverPosition or radiusStep > maxRadiusSteps
return coverPosition
end
function NPC:Cover()
local coverpos = findCover(self.Root,self.Target)
self:Flank(coverpos)
print(self.NPC.Name .. " Is Covering")
end
function NPC:CheckTarget()
local target = self.Target
if target and target.Parent and target.Parent.Humanoid.Health>0 then
return true
end
self.Target = nil
self.Priority = nil
return false
end
function NPC:GetNormalAnims(AnimSet)
for _,v in ipairs(game.ReplicatedStorage.Animations.Thirdperson:FindFirstChild(AnimSet):GetChildren()) do
if v:IsA('Animation') then
self.ThirdMain[v.Name] = self.NPC.Humanoid.Animator:LoadAnimation(v)
end
end
end
function NPC:GetAnims(Weapon)
for _,v in pairs(self.ThirdMain) do
v:Stop()
v:Destroy()
end
local SlotStats = require(game.ReplicatedStorage.gunstats.RangedWeaponStats)[Weapon]
for _,v in ipairs(game.ReplicatedStorage.Animations.Thirdperson:FindFirstChild(SlotStats.AnimationType):GetChildren()) do
if v:IsA('Animation') then
self.ThirdMain[v.Name] = self.NPC.Humanoid.Animator:LoadAnimation(v)
end
end
local KeyframeReached2 = nil
KeyframeReached2 = self.ThirdMain.Hold.KeyframeReached:Connect(function(keyframe)
if keyframe == 'Stop' then
self.ThirdMain.Hold.TimePosition = self.ThirdMain.Hold:GetTimeOfKeyframe('Stop')
self.ThirdMain.Hold:AdjustSpeed(0)
self.ThirdMain.Hold.Priority = Enum.AnimationPriority.Action
KeyframeReached2:Disconnect()
end
end)
for i,v in pairs(game.ReplicatedStorage.Animations.Thirdperson.Death:GetChildren()) do
if v:IsA("Animation") then
self.ThirdMain[v.Name] = self.NPC.Humanoid:LoadAnimation(v)
end
end
end
function NPC:PlayAnimation(Animation : Animation,Character : Model)
if self.NPC.PrimaryPart == nil then return end
end
function NPC:StartAnimLocally(anim,Speed,Priority)
events.Animation:FireAllClients(anim,self.NPC,Speed,Priority)
end
function NPC:StartAnimLocallyForCharacter(Character,anim,Speed,Priority)
events.Animation:FireAllClients(anim,Character,Speed,Priority)
end
function NPC.raycast(origin,dir,character)
local raycastParams = RaycastParams.new()
--
raycastParams.FilterDescendantsInstances = {character}
local result = workspace:Raycast(origin,dir,raycastParams)
if result then
return true, result
else
return false
end
end
function NPC:SicklerAlert(Character,MAIN)
Character.Humanoid.WalkSpeed = 0.001
MAIN.PlaySound("shadow","alert",Character.HumanoidRootPart)
MAIN.PlaySound("shadow","sickler_song",Character.HumanoidRootPart)
task.wait(6.4)
MAIN.PlaySound("shadow","chainbreak",Character.HumanoidRootPart)
self:stopanimations()
Character.Animate2.Enabled = true
self.Range = 950
Character.Humanoid.WalkSpeed = 49
end
function NPC:FlareAnger(Character,MAIN)
Character.Humanoid.WalkSpeed = 0.001
MAIN.PlaySound("shadow","alert",Character.HumanoidRootPart)
MAIN.PlaySound("shadow","sickler_song",Character.HumanoidRootPart)
task.wait(6.4)
MAIN.PlaySound("shadow","chainbreak",Character.HumanoidRootPart)
self:stopanimations()
Character.Animate2.Enabled = true
self.Range = 950
Character.Humanoid.WalkSpeed = 49
end
function NPC:GetTarget()
if not self:CheckTarget() then return end
return self.Target.Position
end
function NPC:GetDist(dist)
if not self:CheckTarget() then return end
return self.Target.Position + (self.Root.Position - self.Target.Position).Unit * dist
end
return NPC
Npc Serverside
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Character = script.Parent
local Modules = ReplicatedStorage.Folder.modules
local NPC_Module = require(Modules.NPC_Module)
local MAIN = require(Modules.Main)
local target
local MoveDebounce = false
local states = {combat = true,seen = false,aggro = false,canattack = false,attackcycle = 2}
local CombatDebounce = false
local origin = Character.PrimaryPart.Position
local picked = nil
local combo = 0
local phase2 = false
local walkspeed = Character.Humanoid.WalkSpeed
function BindableExecution()
MAIN.PlaySound("songs","Battlejuice - Dragon Hunt",workspace)
MAIN.StartAgent("heavy","heavy2",game.ReplicatedStorage)
local execution = NPC_Module:CreateNPC(Character,3,5000,15000,3,2.3,true)
--
local valuestates = execution:GetNPC().CharacterValues.States
Character.Humanoid.WalkSpeed = execution:GetNPC().Walkspeed* execution.speed/1.2
local function findTarget()
execution:findTarget()
end
execution:GiveWeapon("SSword")
execution:GiveCosmetic(Character,"HairVar")
execution:GiveCosmetic(Character,"LumiP")
execution:GiveCosmetic(Character,"LumiG")
Character.Humanoid.HealthChanged:Connect(function()
if Character.Humanoid.Health < 100 and phase2 == false then
execution.Staggered = true
print("Phase2")
execution:GetNPC().ThirdMain.Hurt:Play()
Character.Humanoid.WalkSpeed = 0
execution.Staggered = true
execution.Walkspeed = 0
execution.SPRINTSPEED = 0
Character.Humanoid.MaxHealth= math.huge
Character.Humanoid.Health = math.huge
task.wait(6.86)
task.spawn(function()
execution.Staggered = false
execution.Walkspeed = 42
execution.SPRINTSPEED = 42
Character.Humanoid.WalkSpeed = 42
execution.COMBATDEBOUNCE = true
end)
Character.Humanoid.MaxHealth= 4000
Character.Humanoid.Health = 4000
phase2 = true
execution:GiveCosmetic(Character,"IceFace")
execution:GiveCosmetic(Character,"LumiG1")
execution:GiveCosmetic(Character,"wraithhead",ColorSequence.new(Color3.fromRGB(85, 255, 255)))
execution:GiveWeapon("IceSword")
end
end)
local weaponstats = execution:GiveWeapon(execution:GetNPC().Weapon)
local speed = 0.07 * weaponstats.SpeedRating
local movementset = execution:GetNormalAnims("Movements")
local SpecialSet = execution:GetNormalAnims("Lumi")
--local AnimSet = execution:GetAnims(execution:GetNPC().Weapon)
local shoot =execution:GetNPC().ThirdMain.Swing4
local shoot1
local shoot2
local feint
local combo = 0
local distance = nil
if weaponstats.Ranged then
distance = 10 * weaponstats.SizeRating
elseif weaponstats.Melee then
distance = 3.5
shoot1 =execution:GetNPC().ThirdMain.Swing5
shoot2=execution:GetNPC().ThirdMain.Swing6
shoot.Priority = Enum.AnimationPriority.Action2
shoot1.Priority = Enum.AnimationPriority.Action2
shoot2.Priority = Enum.AnimationPriority.Action2
feint = execution:GetNPC().ThirdMain.feint
feint.Priority = Enum.AnimationPriority.Action3
end
execution:GetNPC().ThirdMain.Hold.Priority = Enum.AnimationPriority.Action
local Heavy
local Grapple
if weaponstats.Melee then
Heavy = execution:GetNPC().ThirdMain.Heavy2
Grapple = execution:GetNPC().ThirdMain.grapple
end
task.wait(1)
execution.REACTIONTIME = 0.5
execution.Walkspeed = 16
execution.SPRINTSPEED = 16
--MAIN.PlaySound("songs","Loose Control",Character.HumanoidRootPart)
task.spawn(function()
while task.wait() do
if execution.NPC and execution.Root and execution.NPC.Humanoid.Health > 0 then
execution:findTarget()
local offset = Character.PrimaryPart.AssemblyLinearVelocity * execution.Ping
if execution.Target then
local cansee = execution:CheckSightToTarget(execution.Target,true)
if cansee then
execution:LookAt(execution.NPC,execution.Target)
if not states.seen then
states.seen = true
task.wait(execution.REACTIONTIME)
if not execution.Target then return end
end
local Goal = execution:GetDist(distance)
if weaponstats.Ranged then
execution:Circle(Goal,20 * distance)
elseif weaponstats.Melee and execution.Target then
execution:Chase(execution.Target.Position + offset + (Character.Torso.Position - execution.Target.Position).Unit * 2.5,5)
end
states.combat = true
if states.combat then
if not valuestates.Stagger.Value then
Character.Humanoid.WalkSpeed = execution:GetNPC().SPRINTSPEED* execution.speed/1.2
else
Character.Humanoid.WalkSpeed = 2
end
task.spawn(function()
if execution.COMBATDEBOUNCE and execution:CalculateDistance() and execution:CalculateDistance() < 200 and weaponstats.SpeedRating < tick() - execution.Tick then
if not execution.Target then return end
if weaponstats.Name == "Benelli M4" or weaponstats.Name == "Remington-870" then
for i = 1, 12 do
execution:Combat("Ranged",9,"Bullet")
end
elseif weaponstats.Name == "SPAS-12" then
for i = 1, 8 do
execution:Combat("Ranged",9,"Bullet")
end
else
if weaponstats.Ranged then
execution:Combat("Ranged",9,"Bullet")
end
end
if weaponstats.Ranged then
shoot:Play(0.1)
MAIN.PlaySound({"weapons",weaponstats.AnimationType},weaponstats.Name,Character.HumanoidRootPart)
elseif weaponstats.Melee and execution:CalculateDistance() and (execution.Root.Position - execution.Target.Position).Magnitude < 6.5 then
if states.attackcycle == 0 then
if phase2 then
if math.random(1,10) == 1 then
execution:Combat("feint",9,"Bullet")
task.spawn(function()
task.wait(0.40)
feint:Play(0.1,5)
end)
else
execution:Combat("Ranged",9,"Bullet")
end
else
execution:Combat("Ranged",9,"Bullet")
end
if combo == 0 then
shoot:Play(0.1,1, 0.75 + speed)
combo += 1
elseif combo == 1 then
shoot:Stop(0.1)
shoot1:Play(0.1,1, 0.75 + speed)
combo += 1
elseif combo == 2 then
shoot2:Play(0.1,5, 0.75 + speed)
combo = 0
states.attackcycle = math.random(0,2)
end
elseif states.attackcycle == 1 then
Heavy:Stop(0.1)
if phase2 then
if math.random(1,10) == 1 then
execution:Combat("feint",9,"Bullet")
task.spawn(function()
task.wait(0.66)
Heavy:Stop(0.1)
feint:Play(0.1,5)
end)
else
execution:Combat("Charged",9,"Bullet")
end
else
execution:Combat("Charged",9,"Bullet")
end
Heavy:Play(0.1,1,.75 + speed)
Heavy.Priority = Enum.AnimationPriority.Action3
states.attackcycle = math.random(0,2)
elseif states.attackcycle == 2 then
Heavy:Stop(0.1)
execution:Combat("grapple",9,"Bullet")
Grapple:Play(0.1,1, 0.95 + speed)
Grapple.Priority = Enum.AnimationPriority.Action3
states.attackcycle = math.random(0,1)
end
end
end
end)
end
if Random.new():NextInteger(1,30) == 30 and execution.COMBATDEBOUNCE then
execution:roll()
end
if Random.new():NextInteger(1,15) == 15 and execution.COMBATDEBOUNCE then
execution:Hop()
end
end
if not cansee then
local Goal = execution:GetDist(distance)
states.seen = false
if not states.combat then
if not valuestates.Stagger.Value then
Character.Humanoid.WalkSpeed = execution:GetNPC().Walkspeed* execution.speed/1.2
else
Character.Humanoid.WalkSpeed = 2
end
else
execution:LookAt(execution.NPC,execution.Target)
if not valuestates.Stagger.Value then
Character.Humanoid.WalkSpeed = execution:GetNPC().SPRINTSPEED*execution.speed/1.2
else
Character.Humanoid.WalkSpeed = 2
end
if weaponstats.Ranged then
execution:Flank(Goal,300)
elseif weaponstats.Melee then
execution:WalkToPosition(Goal)
end
end
end
else
states.seen = false
execution.CANSEE = false
end
end
end
end)
end
BindableExecution()