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

01--delcaring what torso is
02local torso = script.Parent.Torso
03local x = script.Parent.Humanoid
04--finding t he player
05function findPlayer()
06    for _,z in next, game.Players:GetPlayerss()do
07        if z.Character then
08            local char = z.Character
09            if char:FindFirstChild("Humanoid") and char:FindFirstChild("Torso") then
10                local ptorso = char.Torso
11                if (ptorso.Position - torso.Position).magnitude <=30 then
12                    return z
13                end
14            end
15        end
View all 27 lines...

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:

01local aihumroot = script.Parent.HumanoidRootPart
02local aihum = script.Parent.Humanoid
03 
04while true do
05    for _,plr in pairs(game.Players:GetPlayers()) do
06        if plr.Character then
07            local char = plr.Character
08            if (char.HumanoidRootPart.Position-aihumroot.Position).magnitude <= 30 then
09                aihum:MoveTo(char.HumanoidRootPart.Position)
10            end
11        end
12    end
13    wait()
14end

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

1while wait() do
2    local player = findPlayer()
3    if player ~= nil then
4        x:MoveTo(player.Character.Torso.Position)
5    else
6        return nil
7    end
8end

"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:

1if 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:

1function findPlayer()
2    for _,z in next, game.Players:GetPlayers()do
3    print("1")
4        if z.Character then
5    print("2")
6            local char = z.Character
7            if char:FindFirstChild("Humanoid") and char:FindFirstChild("Torso") then
8    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!

1game.Players.PlayerAdded:Connect(function(p)
2while true do
3workspace.Human:SetPrimaryPartCFrame(p.Character.HumanoidRootPart.Position)
4wait(.5)
5end
6end)
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