I’m having trouble making this npc aim towards the nearest zombie, as well as making it shoot at it, so far it just shoots in one area, I have no clue on how I should do this, and tried to search up tutorials on youtube. Script
local tool = script.Parent
local range = script.Parent.Parent.Stats.range.Value
local damage = script.Parent.Parent.Stats.damage
local Bullet = Instance.new("Part", workspace)
local ShootSpeed = script.Parent.Parent.Stats.shootspeed.Value
local Sound = script.Parent.Sound
function shoot()
Bullet.Transparency = 0
Bullet.CanCollide = true
Bullet.Position = script.Parent.Handle.Position
Bullet.Anchored = true
Bullet.Name = "Police1Bullet"
Bullet.BrickColor = BrickColor.new("New Yeller")
Bullet.TopSurface = Enum.SurfaceType.Smooth
Bullet.BottomSurface = Enum.SurfaceType.Smooth
Bullet.Size = Vector3.new(0.3,0.1,0.1)
Bullet.Orientation = Vector3.new(0, 90, 0)
Bullet.Parent = tool
for yes = 1,range do
wait(0.01)
Bullet.Position = Bullet.Position + Vector3.new(0,0,-1)
end
Bullet.Transparency = 1
Bullet.CanCollide = false
end
while wait(ShootSpeed) do
Sound:Play()
shoot()
end
If it matters, script for zombies:
FastZombie = script.Parent.Humanoid
local Health = script.Parent.Values.Health.Value
FastZombie.WalkSpeed = script.Parent.Values.Speed.Value
function onTouched(hit)
if (hit.Name == "Police1Bullet") then
Health = Health - 1
wait(0.1)
if Health < 1 then
script.Parent:Destroy()
end
end
end
connection = FastZombie.Touched:connect(onTouched)
local physicsService = game:GetService("PhysicsService")
physicsService:CreateCollisionGroup("Zombies")
physicsService:CollisionGroupSetCollidable("Zombies", "Zombies", false)
local Zombie = game.ReplicatedStorage.Enemies.Zombie
local FastZombie = game.ReplicatedStorage.Enemies["Fast Zombie"]
local BigZombie = game.ReplicatedStorage.Enemies["Big Zombie"]
local MarbleZombieBoss = game.ReplicatedStorage.Enemies["Marble Zombie"]
local Part1 = game.Workspace.Part1
local Part2= game.Workspace.Part2
local Part3 = game.Workspace.Part3
local Part4 = game.Workspace.Part4
local dead = false
local HealthEvent = game.ReplicatedStorage.Events.HealthEvent
function wave(zombieType, numberOfZombies)
for index = 1, numberOfZombies do
spawn(function()
local ZombieClone = zombieType:Clone()
for _, part in ipairs(ZombieClone:GetDescendants()) do
if part:IsA("BasePart") then
physicsService:SetPartCollisionGroup(part, "Zombies")
end
end
ZombieClone:SetPrimaryPartCFrame(CFrame.new(-108.33, 0.5, -27.21))
ZombieClone.Parent = workspace
wait(1)
ZombieClone.Humanoid:MoveTo(Part1.Position)
ZombieClone.Humanoid.MoveToFinished:Wait()
ZombieClone.Humanoid:MoveTo(Part2.Position)
ZombieClone.Humanoid.MoveToFinished:Wait()
ZombieClone.Humanoid:MoveTo(Part3.Position)
ZombieClone.Humanoid.MoveToFinished:Wait()
ZombieClone.Humanoid:MoveTo(Part4.Position)
ZombieClone.Humanoid.MoveToFinished:Wait()
ZombieClone.Humanoid:Destroy()
wait(1)
ZombieClone:Destroy()
end)
wait(.10) -- Delay Inbetween Spawning Zombies (Remove if you want)
end
end
wave(FastZombie, 5)
wave(Zombie, 5)
wave(BigZombie,2)
wave (MarbleZombieBoss, 1)