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

How to make NPC's use abilities?

Asked by 4 years ago

I'm working on an open-world game where players can go around and kill NPCs. I know how to make regular NPC's that can attack and follow, but I don't know how to make ones that can use abilities. If it helps, here are is an example of what I mean along with the script and the ability I want the NPC to use.

Example (Source: Boku No Roblox): https://gyazo.com/d48f069d1305394bff66a70f7c81cff8

The ability I want the NPC to use (keep in mind that I'm not good at making particles): https://gyazo.com/b65e08d3eb7100bf27efaf02ed05dd7d

Script: https://pastebin.com/00Z9jRvn

0
you can use magnitude to see how close two characters are from each other (NPC.Torso.Position - YourCharacter.Torso.Position).Magnitude royaltoe 5144 — 4y
0
If the two characters are close in distance have the npc attack the character royaltoe 5144 — 4y
0
You can then let the npc use the attack on the character if the character is close enough to the character. royaltoe 5144 — 4y
0
Thanks! Also, would that be local script or script? hihowareya115 53 — 4y
View all comments (2 more)
0
Also, if you don't mind, i'm still new to scripting (2-3 months of experience), could you go into detail? hihowareya115 53 — 4y
0
Sure, do you want to talk on discord? Lucy#0003 royaltoe 5144 — 4y

2 answers

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago

We worked it out on discord. Here's the code if anyone wants to try it out for themselves: I tried to add comments to explain, but comment if you have any more questions.

local effects = game.ReplicatedStorage.Effects
local NPC = script.Parent

function useFireball(playerToAttack)
    --create the fireball
    local fireBall = effects.MagicSkill:Clone()
    fireBall.Position = NPC["Right Arm"].Position
    fireBall.Parent = workspace

    --weld the fireball to the npc's hand and destroy it after .1 seconds to give the effect that it came from the npc's hand. 
    local weld = Instance.new("WeldConstraint", fireBall)
    weld.Part0 = fireBall
    weld.Part1 = NPC.RightArm
    wait(0.1)
    weld:Destroy()


    --give the fireball a velocity.
    --shooting at the direction of the player's head
    local velocity = Instance.new("BodyVelocity", fireBall)
    velocity.Velocity = CFrame.new(fireBall.Position, playerToAttack.Character.Head.Position).LookVector * 100

    --create an event that explodes the fireball when it touches a character
    fireBall.Touched:Connect(function(hit)
        local explosion = effects.Explosion:Clone()

        if hit:IsDescendantOf(NPC)or hit:FindFirstChild("ParticleEmitter") then return end

        explosion.Position = fireBall.Position
        fireBall:Destroy()
        explosion.Parent = workspace

        if hit.Parent:FindFirstChild("Humanoid") then
            hit.Parent:FindFirstChild("Humanoid"):TakeDamage(30)
        end

        wait(5)
        explosion:Destroy() 
    end)
end

while true do
    wait()
    --every 1/30th of a second,
    --loop through the list of players in game
    for i, player in pairs(game.Players:GetChildren())do
        --if that player has a character...
        if player.Character then
            --get the character's distance from the npc
            local distanceFromNPC = (NPC.Torso.Position - player.Character.UpperTorso.Position).Magnitude

            --if the player is 100 studs or closer to the npc
            if distanceFromNPC <= 100 then
                --have the npc attack the player with a fireball
                useFireball(player)
                wait(5)
            end
        end
    end
end
Ad
Log in to vote
0
Answered by 3 years ago

for some reason its not working for me please answer. I got a random npc put a script in him and copy and pasted what u said and i read the script and understoof that i need to make a folder called effects and put a fire ball inside and explosion i did that and still not working please help or anser please

Answer this question