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.
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'
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)