Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Humanoid Ai Problems?

Asked by 9 years ago

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

0
If you cannot find out what is going wrong with a script, use the `print()` statement to output variables and track the progress of code. It will help you troubleshoot when no errors are present. Necrorave 560 — 9y
0
Roger that sir. LateralLace 297 — 9y
0
Have you sorted out things like actually enabling the AI to move? TheDeadlyPanther 2460 — 9y
0
I've tested if the humanoids walktopoint property has worked before. Like I said, if you don't understand, please test it yourself. LateralLace 297 — 9y
View all comments (2 more)
0
You called Repeat() before the function exists, your function Repeat() must be above line 10, where it is called. After that, comment what is happening. Do not forget, you cannot anchor an AI. You also need all necessary parts. alphawolvess 1784 — 9y
0
Id like you to post an answer to my question instead of commenting. I want to make sure I get closure for all of my questions. LateralLace 297 — 9y

1 answer

Log in to vote
1
Answered by
Validark 1580 Snack Break Moderation Voter
9 years ago

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 Vardoesn'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.Humanoidis 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.

0
Thx forthe tips!!! IDK if I have OCD er anything but I like my functions and variables to be kept in the same format. LateralLace 297 — 9y
0
Also there shouldnt be anything wrong with humanoid im using. Yes the model is called "Humanoid", but there is a humanoid object within the model and all the parts ar6e named accordingly (left arm, right arm). LateralLace 297 — 9y
0
Oh, that's weird that you called the model humanoid Validark 1580 — 9y
Ad

Answer this question