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 |
02 | local torso = script.Parent.Torso |
03 | local x = script.Parent.Humanoid |
04 | --finding t he player |
05 | function 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 |
Help would be greatly appreciated! I'm a real newbie when it comes to coding in LUA, haha.
A more simplified script:
01 | local aihumroot = script.Parent.HumanoidRootPart |
02 | local aihum = script.Parent.Humanoid |
03 |
04 | while 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() |
14 | end |
Now to explain what was wrong with your script to better learn, yuh? okcool so
1 | while 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 |
8 | 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:
1 | 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:
1 | function 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'
Here's a mouthful, sorry!
1 | game.Players.PlayerAdded:Connect( function (p) |
2 | while true do |
3 | workspace.Human:SetPrimaryPartCFrame(p.Character.HumanoidRootPart.Position) |
4 | wait(. 5 ) |
5 | end |
6 | end ) |