Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Can someone please help me make a script which only follows players?

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)

10 comments

0

Is your game using R15? If so, this could be the cause, because in your findNearestTorso you check for "Right Arm" which isn't present in R15, but rather "RightLowerArm" and "RightUpperArm"

0

The script you use is outdated, filled with deprecated code. It will for sure break your game.

0

I know it’s not your script, and ScriptingHelpers.org is a site for helping users with self written code, not code from free models.

0

@Kiriot22, how do i check if game used r15. Also would that make it LeftUpperArm and LeftLowerArm too or something? and thx for replying

0

And @incapaz, im sorry but i have no clue when scripting with Lua, and using free models scripts are my only choice at the moment for my game, but i do plan on learning Lua during summer break (properly).  But thx for advice. 

0

oh yeah... kiriot is right. I totally forgot that... it would also explain why if I test a game, in my character it says uper and lower arm instead of just, right arm. But I do think that it will still chase anyone tho.

0

And guys i've still not found an answer, even though i've been researching practically 2 hrs a day about lua and the question :((

0

":connect" is deprecated and ":findFirstChild" is too and your game should be r15

0

How do i change those words then? and i am pretty sure my game is r15 @mudathir2007 . Also guys the script in action is like the NPCs in the game Ro-Ghoul on Roblox, so if u want an idea on what I'm asking of u guys.

0

:connect is :Connect and findFirstChild is FindFirstChild

1 answer

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 :|

1 comment

0

you see the lua figure on the text tab? you press that and it will spawn two rows of tildes, put the code inside of it.

This discussion is preserved as it was. New answers, comments, and votes are closed.