local clone = script.Parent:Clone()
for i,v in ipairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") then
if v:CanSetNetworkOwnership() then
v:SetNetworkOwner(nil)
end
end
end
--Main Body
local MyHuman = script.Parent.Humanoid
local MyHead = script.Parent.Head
local MyTorso = script.Parent.Torso
local Engine = script.Parent.Engine
local BodyBG = MyTorso.BodyGyro
local Status = script.Parent.Status
local BodyColor = script.Parent.TankColor
--Gun Parts
local GunBackMain = script.Parent.GunBackMain
local GunBase = script.Parent.GunBase
local GunWeld = GunBackMain.Weld
local GunTip = script.Parent.GunBarrelTip
local Barrel = script.Parent.Barrel
local BarrelWeld = script.Parent.BarrelWeld
local GunBarrelTube = script.Parent.GunBarrelTube
local GunBarrelTube2 = script.Parent.GunBarrelTube2
local Flash = Barrel.Flash
local SmokeSpot = Barrel.SmokeSpot
local BarrelSmoke = SmokeSpot.Smoke
local Allies = {script.Parent.Name, "Zombies"}
--SFX
local FireSound = Barrel.Shoot
local EngineSound = Engine.EngineNoise
local ExplosionSound = Engine.Explosion
local Sight = script.Sight.Value
local Detection = script.Detection.Value
local FailedPaths = 0
local TouchEvents = {}
local PathArgs = {
["AgentRadius"] = 11,
["AgentHeight"] = 13,
["AgentCanJump"] = false
}
local gunCool = true
local PathBlocked = false
--Initial Adjustments
EngineSound:Play()
BodyBG.CFrame = MyTorso.CFrame
BarrelSmoke.Parent = nil
--Spawn Function to make new threads.
function Spawner(func, ...)
local TempFunc = coroutine.wrap(func)
TempFunc(...)
end
Spawner(function()
repeat
task.wait()
if MyTorso.Velocity.Magnitude > 5 or MyTorso.RotVelocity.Magnitude > 0.1 then
game:GetService("TweenService"):Create(EngineSound, TweenInfo.new(0.2), {PlaybackSpeed = 0.35}):Play()
else
game:GetService("TweenService"):Create(EngineSound, TweenInfo.new(0.2), {PlaybackSpeed = 0.3}):Play()
end
until MyHuman.Health < 1
end)
for i,v in ipairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") and v.Name ~= "HeadLight" and v.Name ~= "Status" then
v.BrickColor = BodyColor.Value
end
end
function checkDist(part1, part2)
return (part1.Position - part2.Position).Magnitude --ERROR HAPPENS HERE
end
function checkSight(target)
local ray = Ray.new(MyTorso.Position, (target.Position - MyTorso.Position).Unit * Sight)
local Hit, Position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
local Ray2 = Ray.new(GunBackMain.Position, (target.Position - GunBackMain.Position).Unit * Sight)
local Hit2, Position2 = workspace:FindPartOnRayWithIgnoreList(Ray2, {script.Parent})
if Hit and Hit2 then
if Hit:IsDescendantOf(target.Parent) and Hit2:IsDescendantOf(target.Parent) then
return true
end
end
return false
end
function FindTarget()
local Dist = Detection
local Target = nil
local PotentialTargets = {}
local seeTargets = {}
for _,v in ipairs(workspace:GetChildren()) do
local Human = v:FindFirstChild("Humanoid")
local Torso = v:FindFirstChild("HumanoidRootPart") or v:FindFirstChild("Torso")
if Human and Torso and v~= script.Parent then
if checkDist(MyTorso, Torso) < Dist and Human.Health > 0 then
for i,x in ipairs(Allies) do
if x == v.Name then
break
elseif i == #Allies then
table.insert(PotentialTargets, Torso)
end
end
end
end
end
for i,v in ipairs(PotentialTargets) do
if checkSight(v) then
table.insert(seeTargets, v)
end
end
for i,v in ipairs(seeTargets) do
local DistanceFromEnemy = checkDist(MyTorso, v)
if DistanceFromEnemy < Dist then
Target = v
Dist = DistanceFromEnemy
end
end
if Target then return Target end
for i,v in ipairs(PotentialTargets) do
if checkDist(MyTorso, v) < Dist and math.abs(MyTorso.Position.Y - v.Position.Y) < 2 then
Target = v
Dist = checkDist(MyTorso, v)
end
end
return Target
end
function Rotate(target)
if checkDist(target, MyTorso) > 3 then
local look = Vector3.new(target.Position.X, MyTorso.Position.Y, target.Position.Z)
local lookDiff = (MyTorso.CFrame.LookVector - CFrame.new(MyTorso.Position, look).LookVector).Magnitude
game:GetService("TweenService"):Create(BodyBG, TweenInfo.new(lookDiff), {CFrame = CFrame.new(MyTorso.Position, look)}):Play()
task.wait(lookDiff)
end
end
function pathToTarget(target)
PathBlocked = false
local Path = game:GetService("PathfindingService"):CreatePath(PathArgs)
Path:ComputeAsync(MyTorso.Position, target.Position)
local Waypoints = Path:GetWaypoints()
if Path.Status == Enum.PathStatus.Success then
FailedPaths = 0
for i,v in ipairs(Waypoints) do
Spawner(Rotate, v)
local Timer = 1
local Success = true
repeat
task.wait()
MyHuman:Move(MyTorso.CFrame.LookVector)
Timer = Timer + 1
if Timer > 40 then
Success = false
break
end
until checkDist(MyTorso, v) < 4
if Success == false or PathBlocked == true then
break
elseif checkDist(Waypoints[#Waypoints], target) > 20 then
break
elseif not target.Parent or target.Parent.Humanoid.Health <= 0 then
break
end
if i % 5 == 0 then
if checkDist(target) and checkDist(MyTorso, target) < 50 then
break
end
end
end
MyHuman:Move(Vector3.new(0,0,0))
else
FailedPaths = FailedPaths + 1
if FailedPaths > 10 then
--get unstuck function
end
end
end
local MainTarget = nil
local TargetHuman = nil
function main()
PathBlocked = false
if MainTarget then
if not checkSight(MainTarget) or not MainTarget.Parent then
MainTarget = nil
end
end
if not MainTarget or TargetHuman.Health <= 0 then
MainTarget = FindTarget()
if MainTarget then
TargetHuman = MainTarget.Parent.Humanoid
end
end
if MainTarget then
if checkDist(MyTorso, MainTarget) < 35 and not PathBlocked then
Rotate(MainTarget)
repeat
MyHuman:Move(MyTorso.CFrame.LookVector)
task.wait()
Spawner(Rotate, main())
until TargetHuman.Health <= 0 or MyTorso.Velocity.Magnitude < 1 or
checkDist(MyTorso, MainTarget) > 35 or PathBlocked
if not PathBlocked then
MyHuman:Move(Vector3.new(0,0,0))
end
elseif checkDist(MyTorso, MainTarget) > 120 or not checkSight(MainTarget) then
pathToTarget(MainTarget)
end
end
end
while task.wait() do
if MyHuman.Health > 0 then
main()
else
break
end
end
For some reason my CheckDist function leads to a index nil, any help with this?#
CheckDist is just a efficent way to prevent spam of checking distances.
The error is highlighted with --ERROR HAPPENS HERE