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

What's the best way of making npcs that will attack players?

Asked by
Echtic 128
4 years ago

So i am having a breakdown, i have no idea how to start with the npc script. I don't need anyone to make the script for me, i need someone who'd either tell me what i'm supposed to do in short notes, or someone who'd give me the dev wiki article link since i couldn't find anything that would help me. I would really appreciate anyone who'd help me learn this, it's quite important.

2
Could probably be done using the Humanoid:MoveTo() function (https://developer.roblox.com/en-us/api-reference/function/Humanoid/MoveTo) for walking and the BasePart.Touched event (https://developer.roblox.com/en-us/api-reference/event/BasePart/Touched) to check if part of the NPC is touching the player. Humanoid:Move() (https://developer.roblox.com/en-us/api-reference/function/Humanoid/Move) may a hpenney2 53 — 4y
0
yep greenhamster1 180 — 4y

2 answers

Log in to vote
2
Answered by 4 years ago

Ive actually also needed help with this but after a while of trying to get help on how to make npc's that fight back i got nothing. so i went and using my own skills created my own script and its pretty simple. To make the script for the npc to fightback you want to make a function play when the npc is touched if script.Parent.Parent.Parent.UpperTorso.Touched then or script.Parent.Parent.Parent.UpperTorso.Touched:Connect(function(hit) and then once this part of the script is done you then want to get a animation to play or you dont have to but i did anyway so when the function plays the npc does a animation where he swings his sword. Next you want to go ahead and make a few lines of script where the npc damages the player. what ive done is i have a npc that is holding a sword so when he swings the sword and it touches a object with a Humanoid it damages it by writing these lines:

local char = hit.Parent --gets the object that touches the npc
local human = char:FindFirstChild("Humanoid") -- getting the humanoid

human.Health = human.Health - 25 -- damages the humanoid

im sure theres another way you can damage the humanoid in a more proper way but this is what ive done and its pretty simple, also this was all done in a serverscipt in the npc.

hope this helped you!

0
also if you are trying to make a rpg type game insert a player following script into the npc so it will follow the player around and attack the player. ahsan666666666666 264 — 4y
0
I do not really suggest using the "Touched" function for it can cause a good amount of lag if not controlled. greenhamster1 180 — 4y
0
That is not entirely true tho I noticed in your answer u mention determine how far away the player are so if you want to you can make it where instead of using touched you can play the function if the player is in a close enough range with the npc ahsan666666666666 264 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

For starters, I would look into raycasting to determine how far away all the players are and if they are in the line of the sight of the NPC https://developer.roblox.com/en-us/articles/Making-a-ray-casting-laser-gun-in-Roblox. I would make a loop that refreshes every two-ish seconds to not have too much lag. There are two ways you can go at moving the NPC. The first one is a simple CFrame which can get a floating effect for an NPC(I don't suggest). The second one would be based around "Humanoid:MoveTo" https://developer.roblox.com/en-us/api-reference/function/Humanoid/MoveTo. Then add some part to your script that makes it so that the NPC plays some sort of animation and for example swings a sword(with detection you can use raycasting). Then to top it all of, make it so that the humanoid breaks joints once it's health is at 0 so that it gives a more realistic death effect. Hope this helps!

0
I would really like to, but sadly i cannot accept both answers, i gave you an upvote, hope you don't mind, thanks for helping it means a lot Echtic 128 — 4y
0
It's all good, glad I can help! greenhamster1 180 — 4y

Answer this question