game.Players.PlayerAdded:connect(function(Player)
local p = game.Workspace:WaitForChild(Player.Name)
print (p)
local f = p.UpperTorso.Position
print (f)
local dist = 15
local pos1 = f
		for i,v in pairs(game.Workspace.CrystalMobA.UndeadWarrior:GetChildren())do
		 if v.Name == "HumanoidRootPart" then
			print (v)
			local z = v.Parent.Humanoid
			local pos2 = v.Position
		    local magnitude = (pos1 - pos2).magnitude
		    if magnitude <=15 and z then
			if p.Humanoid ~= nil then 
				print('humanoid')
				while true do
	wait(1)
	local target = f(v.Position)
	if target ~= nil then
		print ("Following")
		v.Parent.Humanoid:MoveTo(target.Position, target)
						end
					end
				end
			end
		end
	end
end)

3 comments

0

Line 2 is a bad way of getting the character, what if the characters name was "Part" OOF RIP

0

Literally, everybody starting out to script gets their character this way. It's not wrong, but it's very inefficient. A Player object has a property called 'Charcater.' this property is your character, so to get the character, you just have to 'local p = Player.Character'

0

Also, you probably don't need the (v.Position) at line 19, local target = f(v.Position). I think it'll work if you just remove (v.Position).