Yet again I have a another question with humanoid AI ;/ . Bear with me, Ive cerated this script that will find the closest player to a humanoid. Then the humanoids WalkToPoint position will be set as players position. The outputs reads no errors, its just nothing happens. If you dont get it, please copy the script and place a humanoid in your workspace. Here's the script:
diff = 0 comp = 0 Players = game.Players:GetChildren() human = workspace.Humanoid function Var(p1,p2) diff = (p1.Maginitude - p2.Maginitude).Maginitude end for i,v in pairs(Players) do Var = human.Torso.Position , v.Character.Torso.CFrame.p if comp==0 or diff<comp then comp = diff player = v.Character.Torso.CFrame.p Repeat() end end function Repeat() repeat wait(.2) human.Humanoid.WalkToPoint = player until Players==nil end
THX!!
First of all, Players = game.Players:GetChildren()
saves a table to Players
which your repeat until loop will indefinetly check if it stops existing later in the script.
Second of all, function Var
doesn't return. Try the following:
-- Also I renamed it to be more descriptive of what it actually does local function getDist(p1,p2) return (p1.Maginitude - p2.Maginitude).Maginitude end
Third of all, line 11 is in no the correct way of calling a function. Try the following syntax:
local distance = getDist(human.Torso, v.Character)
Also, human = workspace.Humanoid
won't work because Humanoid needs to be inside a character of some sort in order to run a WalkTo command.
Line 15, you called function Repeat() before it exists.
On line 22, human.Humanoid
is the same thing as (substitute human for the variable declaration at the beginning of script) workspace.Humanoid.Humanoid
There is nothing correct about this script. But I will get you started in the right direction:
local diff = 0 local comp = 0 local human = workspace.Character local Humanoid = human.Humanoid local Target local function getDist(p1,p2) return (p1.Maginitude - p2.Maginitude).Maginitude end spawn(function() -- spawn runs a function on a separate thread while true do if Target then human.Humanoid.WalkToPoint = Target wait() end end end) function getTarget() for i,v in pairs(game.Players:GetPlayers()) do if Target == nil and 100 > getDist(human.Torso.Position , v.Character.Torso.CFrame.p) then -- If the distance between the NPC Torso and the Character is less than 100 Target = v.Character.Torso.CFrame.p end end wait(1) getTarget() end
Not guaranteeing the script I gave you is worth anything, but good luck anyway.