I'm trying to make an NPC shoot a fireball at the player every one second when the NPC is chasing the player. I've managed to have the NPC shoot a fireball but it doesn't go toward the player. How do I make a fireball shoot at the player?
Function that makes the NPC chase the player:
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 20
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
Code that activates the function and that creates the fireball (located in the same script):
while true do
wait(0.1)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
wait(1)
script.Parent.Human:MoveTo(target.Position, target)
---Fire ball script
local fireball = script.Parent.Handle:clone()
fireball.CFrame = script.Parent.Torso.CFrame
fireball.Position = script.Parent.Torso.Position
local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(1000,1000,1000)
bv.Velocity = Vector3.new(target.Position.x,0,0)
bv.Parent = fireball
fireball.Anchored = false
fireball.CanCollide = false
fireball.Transparency = 0
fireball.Parent = game.Workspace
game.Debris:AddItem(fireball,2)
local ten = true
fireball.Touched:connect(function(hit)
if not ten then return end
ten = false
local ehum = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
if ehum and ehum ~= script.Parent.Human then
ehum:TakeDamage(25)
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 0
explosion.BlastPressure = 0
explosion.Position = fireball.Position
explosion.Parent = game.Workspace
fireball:Destroy()
elseif hit.Anchored == true and hit.CanCollide == true then
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 0.5
explosion.BlastPressure = 0
explosion.Position = fireball.Position
explosion.Parent = game.Workspace
end
wait(1)
ten= true
end)
end
end

3 comments
Thank you!! I've spent almost a day trying to make this work! I have one little problem though, the fireballs wont hurt the player until it reaches a certain distance, do you know why this happens? If not I could probably figure it out. Thanks a ton!
Nvm fixed it thanks!
Awesome! Glad to help!