Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make an NPC shoot a part at the player?

Asked by 7 years ago

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

1 answer

Log in to vote
1
Answered by
gdunn2 54
7 years ago
Edited 7 years ago

Okay, you almost have it. Firstly, change the CFrame of the fireball to, not only the torso's CFrame, but the (the torso's position , the nearest torso's position). That will make the fireball point towards the player's torso. Then, when making the body velocity, make the velocity fireball.CFrame.lookVector. Multiply it by a variable of your choice. The higher the variable, the faster the speed. Test it out and figure what variable works for your situation. I think that should work.

0
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! roblox99456789 104 — 7y
0
Nvm fixed it thanks! roblox99456789 104 — 7y
0
Awesome! Glad to help! gdunn2 54 — 7y
Ad

Answer this question