Trying To Get Summoned NPC To Not Follow It's Summoner

So I’m trying to make an NPC that you can summon to fight other players, so far I have gotten it to follow the player in general thanks to FusionOBC in the forums helping me out. ^ ^

Issue being, I’m trying to make it so that the NPC doesn’t follow it’s own creator. Now I do have a good idea as to how to get this to actually work, I have two values in the summoned NPC itself; the ‘owner’ value (ObjValue, will be used for the attacks so that they don’t damage the creator), and the ‘owner2’ value (StringValue, I use this type of value for homing attacks so that they don’t target the creator either, I don’t know why only specifically StringValue works for homing attacks but it does)

Here is the code that I have for the Follow script so far:

npc = script.Parent.HumanoidRootPart

function findTorso(pos)
	local closestPlayer, closestMag
	
	for _, player in pairs(game.Players:GetPlayers()) do
		local character = player.Character
		if not character then continue end
		
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		if not humanoidRootPart then continue end
		
		local magnitude = (humanoidRootPart.Position - npc.Position).Magnitude
		
		if not closestMag then
			closestMag = magnitude
			closestPlayer = player
		end
		
		if magnitude >= closestMag then continue end
		
		closestMag = magnitude
		closestPlayer = player
	end
	
	local closestPlayerTorso = closestPlayer.Character:FindFirstChild("Torso")
	return closestPlayerTorso
end

while true do
	task.wait(0.1)
	local torso = findTorso(script.Parent.Torso.Position)
	if torso ~= nil then
		script.Parent.Humanoid:MoveTo(torso.Position)
	end
end
--Thanks again to FusionOBC for supplying the code to make the follow script work! ^ ^

So far I have tried putting:

		local character = player.Character
		if not character then continue end

        if player.Name ~= script.Parent.owner2.Value then continue end
		
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")

…But when I playtested it (I can’t do normal run because the NPC will not follow other NPCs, only other Players and I do not have any testers for this game) with this code, in the console it said “attempt to index nil with ‘Character’” at the line with:

local closestPlayerTorso = closestPlayer.Character:FindFirstChild("Torso")

(This is the same error that shows up in console whenever I try running because the other NPCs on the map are not classified as Players)

Now let it be known that I did not put anything into the owner values yet, but still I would greatly appreciate any help with this dilemma I’m having with this code, thank you! ^ ^

“attempt to index nil with ‘Character’” in this context means that the variable “closestPlayer” is nil. Within your code, the only time you set the value of “closestPlayer” is at the end of your loop.
This is a problem because of this line:
if player.Name ~= script.Parent.owner2.Value then continue end
This line causes the loop to end if the player’s name is not equal to the owner’s name, and as you mentioned the owner’s name is nil. This means that the loop will always end upon reaching this line, since the player’s name will always not be nil.

1 Like

UPDATE: Thank you for the feedback! ^ ^
I tried putting that line of code at the very end of the function script:

		closestMag = magnitude
		closestPlayer = player
		if player.Name ~= script.Parent.owner2.Value then continue end

…Then I went to playtest the game, I switched to client version so I could go into the command bar to manually set the owner values to my own name then flipped the Follow script back on, the summoned NPC was STILL following me despite the script saying not to? No errors showed up in the console either which is weird I think.

Maybe I need to rearrange parts of the original code itself?

It follows you because you putted code which is (if player’s name is not equal to owner’s name then continue at start of loop)
and if it’s equal to owner then it follow it’s owner (that’s why).

Either make it if equal or reverse
and also make check in end of function (to not have Attempt to index nil with 'Character')
(if not closest player then return nil).

You need add something like who is target because when i changed the code in correct way, i found another issue with MoveTo() that it doesn’t stop to last position of the target (closest player) althought this can slide away since it’s optional

1 Like

UPDATE: I added a check to the end of the script so now it doesn’t error in the console whenever I test it, and I also reversed the player.Name and script.Parent.owner2.Value thing in the code but when I changed the owner2 value to my name through the client version of playtesting it still ended up following me.

Thank you for helping me fix the erroring in the console in regards to playtesting the script without disabling it first, but I am still having issues with the NPC still following me despite my name being in the owner2.Value in which it shouldn’t be following me.

make it type of equal and that’s it

and don’t forget if it updates value (althought in code it is updates since gets value)

1 Like

UPDATE AGAIN: Wait nevermind, I did the equal to owner thing and put it in between the first two ‘character’ and ‘humanoidrootpart’ things in the code and it actually worked! Thank you so much! ^ ^

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.