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

How to make mini npc's follow you?

So this is my script on what I did to make some npcs to follow me. But when I tested it all the npc did was fling then get up and he didn't follow me.

players = nil; dummy = game.Workspace.CitizenLvl1; currentPlayerFollowing = nil;

while wait(1) do players = game.Players:GetChildren();

for i = 0, table.getn(players), 1 do if (players[i] ~= nil and (math.abs(players[i].Character.Torso.Position.X - dummy.Torso.Position.X)) <= 10) then
currentPlayerFollowing = players[i]; m end end

if (currentPlayerFollowing ~= nil) then dummy.Humanoid.WalkToPoint = currentPlayerFollowing.Character.Torso.Position end end?

So anything I can do to fix it?

1 answer

I recently had this issue aswell, but only because I had never used npc's. Changing their walktopoint won't do anything, (No reason why)... But moveto() will. If your npc is flinging, it is likely because it is connected to the baseplate, try changing the npc's legs bottom surface to "Smooth" or "SmoothNoOutlines".

Requested Fix Code:

players = nil; dummy = game.Workspace.CitizenLvl1; currentPlayerFollowing = nil;

while wait(1) do 
	players = game.Players:GetChildren();
	for i = 0, table.getn(players), 1 do 
		if (players[i] ~= nil and (math.abs(players[i].Character.Torso.Position.X - dummy.Torso.Position.X)) <= 10) then --[[ It would seem also, you are not getting the magnitude here. See improved corrections below, since it will only go if the x and not z is closer. ]]
			currentPlayerFollowing = players[i]; --[[m]] --[[ Is 'm' intended here? ]]
		end 
	end
	
	if (currentPlayerFollowing ~= nil) then 
		dummy.Humanoid:MoveTo(currentPlayerFollowing.Character.Torso.Position)
	end 
end

Now, as far as improved code goes, I will now provide extra details to your code.

Improved+Details:

players,dummy,currentPlayerFollowing = nil,game.Workspace:WaitForChild("CitizenLvl1"),nil --[[ You can separate variables by commas and use them in one quick and easy variable list. x,y,z = a,b,c  x will be a, y will be b, z will be c. ]]

while wait(1) do
	players= game.Players:GetChildren()
	for i=1,#players,1 do
		if (players[i]~=nil and (players[i].Character.Torso.Position - dummy.Torso.Position).magnitude<= 10 then --[[ Magnitude will detect if the player is within a 10 stud radius, in this case.]]
			currentPlayerFollowing= players[i]
		end
	end

	if (currentPlayerFollowing~=nil) then
		dummy.Humanoid:MoveTo(currentPlayerFollowing.Character.Torso.Position) --[[ MoveTo is like changing the WalkToPoint, but it actually works... ]]
	end
end

Improved(RAW):

players,dummy,currentPlayerFollowing = nil,game.Workspace:WaitForChild("CitizenLvl1"),nil

while wait(1) do
	players= game.Players:GetChildren()
	for i=1,#players,1 do
		if (players[i]~=nil and (players[i].Character.Torso.Position - dummy.Torso.Position).magnitude<= 10 then
			currentPlayerFollowing= players[i]
		end
	end

	if (currentPlayerFollowing~=nil) then
		dummy.Humanoid:MoveTo(currentPlayerFollowing.Character.Torso.Position)
	end
end

~Taryo

1 comment

0

Thanks Taryo it worked!

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