So in my game I’m trying to make a move for one of the characters where they’ll summon an NPC to fight other players, right now I’m trying to make the code for it that makes it follow other players.
Right now the goal is to make it follow players from a 500 stud distance, while also stopping at a certain minimum distance so that way they don’t just constantly bump into the player if too close.
While I have gotten the NPC to follow people, it just does not seem to work the way I have intended; in the script I have it so that the closest person nearby will be set as the NPC’s ‘targetlock’ value so that way it will only go after that person but when that person dies it’s ‘targetlock’ npc will change to the nearest alive person instead, but it just seems to completely ignore the value and go for the closest thing instead which, that is what I want but for some reason the value itself has itself set to a completely different person on the map (I am currently testing this with multiple dummy NPCs), like every single time it would always default to this one specific NPC that wasn’t even the closest one to the summoned NPC. When I did either move that specific NPC so far away that it wouldn’t register or just flatout deleted it, it would STILL default to another random specific NPC that wasn’t even the closest one to the summoned NPC.
I haven’t even delved into the minimum distance thing yet because I’m still trying to figure out how to get the normal targetting system to even work, with the minimum distance thing in set it will go after one target before immediately switching to the other once inside the minimum distance threshold.
Here is the code for it:
function findTorso(pos)
local torso = nil
local dist = 500 --Maximum distance
local mindist = 50 --Minimum distance, haven't implemented this into the code yet
local child = workspace:children()
for i=1, #child do
if child[i].className == "Model" then
local h = child[i]:FindFirstChild("Humanoid")
if h ~= nil then
local check = child[i]:FindFirstChild("Torso")
if check ~= nil and check.Parent.Name ~= script.Parent.owner2.Value and check.Parent.Name ~= script.Parent.Name then --Makes it so that the NPC won't try to follow it's owner or itself
script.Parent.targetlock.Value = check.Parent.Name --Sets it's current follow target
if (check.Position - pos).magnitude < dist then
torso = check
dist = (check.Position - pos).magnitude
if check.Parent.Humanoid.Health <= 0 then --If current target is dead then choose another one
script.Parent.targetlock.Value = check.Parent.Name
end
end
end
end
end
end
return torso
end
while true do
task.wait()
local torso = findTorso(script.Parent.Torso.Position)
if torso ~= nil then
script.Parent.Humanoid:MoveTo(torso.Position) --Makes the NPC move to it's target
end
end
So far I have tried doing:
if torso ~= nil and torso == script.Parent.targetlock.Value then
script.Parent.Humanoid:MoveTo(torso.Position)
end
…but that just seems to brick the NPC’s movement completely no matter what iteration of those lines of code I tried doing.
I’ve also tried messing around with various parts of the findTorso() function itself like moving the:
check.Parent.Name == script.Parent.targetlock.Value
line of code around to various parts of the code where applicable but that didn’t seem to make a difference at all.
Overall the main issue is that it is following the closest NPC, but the ‘targetlock’ value (which is a StringValue, I’ve tried it with ObjectValue too but it seemed to have the exact same problems) keeps setting itself to a specific NPC that isn’t even the closest one to the summoned NPC.
This is a problem because if the targetlock value isn’t working, then the minimum distance thing will continue to do that thing where the summoned NPC just goes back and forth with multiple different NPCs on the map without ever locking onto one specific one that it’s trying to target.
If anybody could offer me some help with this dilemma then that would be greatly appreciated, thank you! ^ ^