Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Scripting an Enemy that follows a player?

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.

5 comments

0

in game.Players:GetPlayers() you got a typo

0

I changed it to "GetPlayers()" removing that double s, but it seems to remain stagnant for some reason :S

0

Try changing the torso to UpperTorso in line 9

0

Thanks! Its still stationary..do the roblox bodies effect how the code operates?

0

Based it off or copied it?

2 answers

✓ Accepted answer

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'

1 comment

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!

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)

1 comment

0

where exactly should I place this in my code? should i replace my "while wait do ()" with this?

This discussion is preserved as it was. New answers, comments, and votes are closed.