I've searched all over the internet for a follow player only script, but they either don't work or are outdated. The NPCs attack each other not players (they're ignored) Any help is much appreciated (Please also explain the script to me, as i'm a total newbie at Lua scripting) This is my current follow script :-
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 = 1000 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("Right Arm") 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 while true do wait(math.random(1,5)) local target = findNearestTorso(script.Parent.Torso.Position) if target ~= nil then script.Parent.Humanoid:MoveTo(target.Position, target) end end
I wonder if my damage script does any difference to the NPCs actions but i'll put it here anyways :-
local rarm = script.Parent:FindFirstChild("Right Arm") local larm = script.Parent:FindFirstChild("Left Arm") function dmg(hit) if hit.Parent ~= nil then local hum = hit.Parent:findFirstChild("Humanoid") if hum ~= nil then hum.Health = hum.Health -10 end end end rarm.Touched:connect(dmg) larm.Touched:connect(dmg)
Hello you English,
I am also a newbie at scripting(but I'm a big doofus as I'm making an EXTREME complicated game :|) but ill do my best to answer your question :D.
What I understand from your damage script is that when one of your NPC touches something, the humanoid that was touched by your NPC gets damaged by 10. if so, then the script seems good to me except for one thing. I don't know if it is needed but...
rarm.Touched:connect(dmg) larm.Touched:connect(dmg)
is missing the function and should be more like this...
rarm.Touched:connect(function(dmg) larm.Touched:connect(function(dmg)
I hope that would fix your script and it maybe could be why your NPC attack each other... maybe?
Another thing. In the first script, you should tag your enemies and players and make the enemies only search for specific tagged players.
Basically...you need to add this in the first script at the top.
local PlayerTag = instance.new("stringValue")
PlayerTag.Name = "Player"
Game.player.PlayerAdded:connect(function) PlayerTag.Clone().Parent = game.players.localPlayer end)
Then, go in the torso of all your enemies and add a stringValue, name it Enemy. For now, what all this does is, add a stringValue to the torso of all your enemies and players. This string value is going to be your tag. Then, cut the 34th line(make sure its cut, so you can paste it back after).After this, under line 33, write the following.
local Tag = findNearestTorso(script.Parent.Torso) if Tag.Player ~= nil then --paste the cut live under me
end
and voila. It should work... maybe... hopefully? I hope I was any help, and I'm sorry if the script doesn't work. As I said earlier, I'm also a total newbie at scripting.
Kind regards. -Nachsor
P.S.Can someone tell me how to make it show its Lua code, please, its always random with me :|