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

Unable to get NPC to move to player?

Asked by 4 years ago
Edited 4 years ago

I have a script inside an NPC's HumanoidRootPart which is supposed to check if a player hit the NPC and that makes the NPC follow him. Here it is:

function onTouched(hit)
if script.Parent.Parent.Humanoid.Health < 100 and hit.Parent.ClassName == 'Tool' then -- checks if it's a tool and if the NPC has been damaged
script.Parent.Value.Value = true -- don't mind that
while hit.Parent.Parent.Parent.Humanoid.Health > 0 do -- checks if player is still alive
    wait(.5)
script.Parent.Parent.Humanoid:MoveTo(Vector3.new(hit.Parent.Parent.HumanoidRootPart))
end
end
end
script.Parent.Touched:connect(onTouched)

However, it didn't seem to work so I made the script print the hit.Parent.Parent.Name to check if I was actually targeting the character and sometimes it gives me my character name, other times Workspace and other times backpack. Is there any way to fix this?

Also, I'm unsure if the WalkTo works.

0
ok so yumaking 78 — 4y
0
so i did a quick test in studio with your script and the first problem I found is that on Line 4 you say hit.Parent.Parent.Parent that is you put dot parent 3 times which explains why it gave you workspace hit.Parent is the tool the parent of that is the character model if you add another dot parent, you get an error saying HumanoidRootPart is not a valid member of Workspace, so that is a problem yumaking 78 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

Ok so this is how I would do it if I had to :

script.Parent.Touched:Connect(function(hit)
while wait() do --while loop to keep moving to player even if their position changes
if hit.Parent:IsA("Tool") and script.Parent.Parent.Humanoid.Health<100 then
--checks if npc was hit by a tool
local tool=hit.Parent
local char=tool.Parent
local me=script.Parent.Parent.Humanoid
print(char.Name)
local altchar

if char.Name~="Backpack" then-- checks if tool is in the backpack, if no then it goes to the char
me:MoveTo(char.HumanoidRootPart.Position)

elseif char.Name=="Backpack" then -- if tool is in backpack it references the player object and gets character and moves to it,this way if the player puts their tool in the backpack and therefore the tools parent is no longer the player character the npc still comes for them
altchar=char.Parent.Character
local me=script.Parent.Parent.Humanoid
me:MoveTo(altchar.HumanoidRootPart.Position)
end


--this was personal for me to check if it hit something that is not a player
else print("first no")
end
end
end)

The code was longer than I thought but by the way I tested it in studio and it worked inside a humanoid root part of a dummy I used

Also you might want to check if the player is far away from the npc and stop them if they are, so you can use magnitude for that like this:

local char= --some reference to the character model like tool.Parent or whatever is your case
local npc= --some reference to the npc model like script.Parent.Parent or whatever suits your needs
local aNumber= --[the max distance an npc can chase a player from(can be whatever you want, I think it is measured in studs)

--if the npc's position-the player's position(distance between them) is greater than whatever number you put in then do some code, I am assuming you want to stop the npc from chasing the player 
if (npc.HumanoidRootPart.Position-char.HumanoidRootPart.Position).magnitude>aNumber 
 then
break --breaks out of the loop that is making npc chase player
end

keep in mind the reference of your char will change if they put the tool in their back pack so your going to want to have a variable for altchar that references the backpack's parent. It might be like(also I am assuming you already have the stuff I put int he code snippits above):

if (npc.HumanoidRootPart.Position-char.HumanoidRootPart.Position).magnitude>aNumber or
(npc.HumanoidRootPart.Position-altchar.HumanoidRootPart.Position).magnitude
--keep in mind altchar is equal to local altchar=char.Parent.Character because char is the tool's parent and if the tool is in the backpack its parent is backpack whose parent is player
so we can say player.Character and in our case char.Parent.Character
 then
break --breaks out of the loop that is making npc chase player
end

that is all I am sorry this answer was so long but it is what it is, I hope it helped.

1
Great answer! Tysm :D eloiishot 47 — 4y
0
Happy I could help ???????? yumaking 78 — 4y
Ad

Answer this question