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! ^ ^