NPC shooting "fireball" at player current position Issue

Alright, fellas, I got an issue.

Right now what I am trying to do is that I want an NPC to fire a part or “Fireball” at the nearest player position. So far I got the fireball to spawn and shoot.

But it won’t go in the direction of the player, it’ll just shoot at the ground from its position which is the NPC’s right hand.

I don’t want a homing missile either. Just fire it at the nearest player’s exact location.

My big dumb brain can’t seem to figure this issue out for like a day. Never was good at making projectiles.

local Fired = true
spawn(function()
	while true do
		wait()

		local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)

		if target ~= nil then
			wait(5)
			
		

			
			local fireball = game.ReplicatedStorage.Fireball:Clone()
			fireball.CFrame = target.CFrame
			fireball.Position = script.Parent.RightHand.Position
			
			local bp = Instance.new("BodyPosition")
			bp.MaxForce = Vector3.new(math.max,math.max,math.max)
			bp.Position = Vector3.new(target.Position.X,target.Position.Y,target.Position.Z)
			bp.Parent = fireball
			
			local bv = Instance.new("BodyVelocity")
			bv.MaxForce = Vector3.new(math.max,math.max,math.max)
			bv.Velocity = Vector3.new(target.Position.X,target.Position.Y,target.Position.Z)
			bv.Parent = fireball

			fireball.Anchored = false
			fireball.CanCollide = false
			fireball.Transparency = 0
			fireball.Fly:Play()
			fireball.Parent = game.Workspace
			game.Debris:AddItem(fireball,50) 



		wait(1)
			Fired = true

			fireball.Touched:connect(function(hit)
				
				if not Fired then return end
				Fired = false
				local hum = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
				local zombie = hit.Parent:FindFirstChild("Zombie") or hit.Parent.Parent:FindFirstChild("Zombie")
				if hum then
					hum:TakeDamage(25)
					fireball.Hit:Play()
					fireball.Fly:Stop()
					fireball.BodyVelocity:Destroy()
					fireball.Anchored=true
					wait(fireball.Hit.TimeLength)
					fireball:Destroy()
					
				elseif zombie then
					
					zombie:TakeDamage(25)
					fireball.Hit:Play()
					fireball.Fly:Stop()
					fireball.BodyVelocity:Destroy()
					fireball.Anchored=true
					wait(fireball.Hit.TimeLength)
					fireball:Destroy()
					
					
				else 
					fireball.Hit:Play()
					fireball.Fly:Stop()
					fireball.BodyVelocity:Destroy()
					fireball.Anchored=true
					wait(fireball.Hit.TimeLength)
					fireball:Destroy()

				end

				wait(1)
				Fired= true
			end)


		end
	end
	
	
end)

Thanks.

Question: Do you know about RayCasting or what it is? That can help you with your case that you have rn!

I have no clue, never used raycasting before.

Get the proper direction vector from the NPC position towards the target position.

local npcPosition = script.Parent.HumanoidRootPart.Position
local bodyVelocityDirection = target.Position - npcPosition
Full code
local Debris = game:GetService("Debris")
local FIRE_BALL_SPEED = 1000

local Fired = true
spawn(function()
	while true do
        wait()
        local npcPosition = script.Parent.HumanoidRootPart.Position
		local target = findNearestTorso(npcPosition)
		if target ~= nil then
			wait(5)
			local fireball = game.ReplicatedStorage.Fireball:Clone()
			fireball.CFrame = target.CFrame
			fireball.Position = script.Parent.RightHand.Position
						
			local bv = Instance.new("BodyVelocity")
            bv.MaxForce = Vector3.new(math.max,math.max,math.max)
            local bodyVelocityDirection = target.Position - npcPosition
			bv.Velocity = bodyVelocityDirection.Unit*FIRE_BALL_SPEED
			bv.Parent = fireball

			fireball.Anchored = false
            fireball.CanCollide = false
            fireball.Massless = true
			fireball.Transparency = 0
			fireball.Fly:Play()
			fireball.Parent = game.Workspace
			Debris:AddItem(fireball,50) 
		    wait(1)
			Fired = true
			fireball.Touched:connect(function(hit)
				if not Fired then return end
				Fired = false
				local hum = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
				local zombie = hit.Parent:FindFirstChild("Zombie") or hit.Parent.Parent:FindFirstChild("Zombie")
				if hum then
					hum:TakeDamage(25)
					fireball.Hit:Play()
					fireball.Fly:Stop()
					fireball.BodyVelocity:Destroy()
					fireball.Anchored=true
					wait(fireball.Hit.TimeLength)
					fireball:Destroy()
				elseif zombie then
					zombie:TakeDamage(25)
					fireball.Hit:Play()
					fireball.Fly:Stop()
					fireball.BodyVelocity:Destroy()
					fireball.Anchored=true
					wait(fireball.Hit.TimeLength)
					fireball:Destroy()
				else 
					fireball.Hit:Play()
					fireball.Fly:Stop()
					fireball.BodyVelocity:Destroy()
					fireball.Anchored=true
					wait(fireball.Hit.TimeLength)
					fireball:Destroy()
				end
				wait(1)
				Fired= true
			end)
		end
	end
end)

Thanks but it didn’t fix the issue? I did what you said and made the changes and it’s still happening. Here’s a clip of it.

Hmm, then we gotta debug it more. Here are some points I suspect.

So projectile is moving down that means:

  1. There is something making it go down, gravity perhaps? Did it have some initial velocity if so set it to zero?
local fireball = game.ReplicatedStorage.Fireball:Clone()
fireball.Velocity = Vector3.new(0,0,0)
  1. The body velocity is making it fly in the wrong direction then there is something wrong with your find nearest torso function.
local target = findNearestTorso(npcPosition)--make sure it works
print("Targetting: ",target.Parent,"Targets Position: ",target.Position)--print debug the target
local bodyVelocityDirection = target.Position - npcPosition

Other then that, I don’t have all the information so I’m just guessing at this point, hope these pointers guide you in the right direction.

1 Like

Thanks for your help. Sadly it didn’t fix the problem, but I’m thankful you tried. This does point me into a better spot though.

Ah, well I’m pretty sure we can do this without RayCasting I think! if you’re wanting to shoot the Fireball in the position where the Player is at, you can get the direction I think!

local Direction =  (target.Position - script.Parent.RightHand.Positon).Unit * 50

And the BodyVelocity:

bv.Velocity = Direction --You could also multiply this value as well if you want it to go faster?

(My internet died at the worst possible time, but maybe you could try this as well? Reee)

Hmm, try using this function for find the nearest players torso instead:

local Players = game:GetService("Players")
local function findNearestTorso(npcPosition : Vector3)
local gamers = Players:GetPlayers()
    local TargetInfo = {nil,100000000}
    for i = 1,#Players do
        local Dist = (gamers[i].Character.Torso.Position - npcPosition).magnitude
        if Dist < TargetInfo[2] then
            TargetInfo = {gamers[i],Dist}
        end
    end
    return TargetInfo[1].Character.Torso
end

Nothing is working, Ugh. This projectile seems to be more complicated than expected. Idk if it has to do with the NPC moving or not or that it is R15. Btw the game in singleplayer. We might be out of options.

Sadly it does not work. Pepehands.

Hm, shouldn’t you use math.huge instead of math.max? That could be the issue? You can print what the MaxForces of the BodyVelocity & Position is as well

Have you tried first breaking up the steps of getting it working.

Put the fireball as a tool, and shoot it, does it go straight forward in the air where you aim? or does it go down…

Slow down the fireball, so you can see where it might start to be going, like does it go forward at all, or does it just appear and go down.

Later, if in NPC, and it does not move, ie stop it from moving, with debug prints does it find you , the player, spit out the things it finds of you, ie ur position. And you are not moving, does that work

then u move, npc doesnt does that work

are you updating where it is tracking to, since both you and the npc are moving…

is it really hitting itself, and ending?

there is this
local hum = hit.Parent:FindFirstChild(“Humanoid”) or hit.Parent.Parent:FindFirstChild(“Humanoid”)
, is it finding itsself?
put in debug print statements, in any loop / function, condition, to see where you are in the code when it is doing it

send the code as you make changes and get results too please, I like fireballs too!

bv.Velocity = CFrame.lookAt(npcRootPart,  player's RootPart).LookVector * speed

this will point the fireball to the player’s position. However, raycasting would be still better for detecting. Sorry for loosed quote but I’m really busy atm.

This is just a summary, not a real code(more of a pseudocode)