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

Scripting an Enemy that follows a player?

Asked by 5 years ago
Edited 5 years ago

Hey all! I'm new here. it's great to meet all of you.

I'm trying to make a script in which the player is being followed by a zombie, however..when I test it in game, the zombie doesn't move at all

I based this off of a tutorial that showed how to make an enemy moving script

--delcaring what torso is
local torso = script.Parent.Torso
local x = script.Parent.Humanoid
--finding t he player
function findPlayer()
    for _,z in next, game.Players:GetPlayerss()do
        if z.Character then
            local char = z.Character
            if char:FindFirstChild("Humanoid") and char:FindFirstChild("Torso") then
                local ptorso = char.Torso
                if (ptorso.Position - torso.Position).magnitude <=30 then
                    return z
                end
            end
        end
        return nil
    end
end
--makes zombie follow NPC
while wait() do
    local player = findPlayer()
    if player ~= nil then
        x:MoveTo(player.Character.Torso.Position)
    else
        return nil
    end
end

Help would be greatly appreciated! I'm a real newbie when it comes to coding in LUA, haha.

0
in game.Players:GetPlayers() you got a typo User#29913 36 — 5y
0
I changed it to "GetPlayers()" removing that double s, but it seems to remain stagnant for some reason :S piemaster347alt 17 — 5y
0
Try changing the torso to UpperTorso in line 9 awesomemode14 68 — 5y
0
Thanks! Its still stationary..do the roblox bodies effect how the code operates? piemaster347alt 17 — 5y
0
Based it off or copied it? hiimgoodpack 2009 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

A more simplified script:

local aihumroot = script.Parent.HumanoidRootPart
local aihum = script.Parent.Humanoid

while true do
    for _,plr in pairs(game.Players:GetPlayers()) do
        if plr.Character then
            local char = plr.Character
            if (char.HumanoidRootPart.Position-aihumroot.Position).magnitude <= 30 then
                aihum:MoveTo(char.HumanoidRootPart.Position)
            end
        end
    end
    wait()
end

Now to explain what was wrong with your script to better learn, yuh? okcool so

while wait() do
    local player = findPlayer()
    if player ~= nil then
        x:MoveTo(player.Character.Torso.Position)
    else
        return nil
    end
end

"A return functions similar to a break in that it stops the current scope and doesn’t run any code below it" From: https://devforum.roblox.com/t/what-does-return-do/239433 So if it doesn't find the player, it'll end the entire loop ending the search for players.

And your script isn't getting past this f statement:

if char:FindFirstChild("Humanoid") and char:FindFirstChild("Torso") then

A good way of debugging is shoving a print after every line that could be stopping the script from running/advancing, example:

function findPlayer()
    for _,z in next, game.Players:GetPlayers()do
    print("1")
        if z.Character then
    print("2")
            local char = z.Character
            if char:FindFirstChild("Humanoid") and char:FindFirstChild("Torso") then
    print("3")

Extras that don't have to do with this, because yes: If you're using an R6 character, use 'Torso' If you're using an R15 character, use 'UpperTorso'

0
Thanks a ton man! I was struggling a lot with figuring out what was going on--I wasn't expecting the return function to cause such an issue! Thank you for the help! piemaster347alt 17 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago

Here's a mouthful, sorry!

game.Players.PlayerAdded:Connect(function(p)
while true do
workspace.Human:SetPrimaryPartCFrame(p.Character.HumanoidRootPart.Position)
wait(.5)
end
end)
0
where exactly should I place this in my code? should i replace my "while wait do ()" with this? piemaster347alt 17 — 5y

Answer this question