So I'm trying to make a Ray casting enemy. The problem is that I have no idea on how to even start with a ray casting enemy and I'm not even sure how to make a regular enemy. Here is a script that I tried to make by adding my own stuff and combining a bunch of FMed scripts. Could you please show me what I'm doing wrong and how I can fix it? (Keep in mind that this script may be cringy asf)

Script:



local PlayersService = game:GetService('Players')

function FindPlayer(pos)
	local list = game.Workspace:children()
	local torso = nil
	local temp = nil
	local human = nil
	local temp2 = nil

	if	game.workspace:FindFirstChild("Humanoid") and game.workspace:FindFirstChild("Humanoid").Health > 1 then
		
	local torso = game.workspace:FindFirstChild("Torso")
	

		for _, player in pairs(PlayersService:GetPlayers()) do
			
			local list = game.Workspace:children()
			
				for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("Torso")
			human = temp2:findFirstChild("Humanoid")

				local torso = player.Character.Torso

				local hitObject, hitPosition = workspace:FindPartOnRay(Ray.new(torso.CFrame.p, torso.CFrame.p + Vector3.new(0, 800, 0)), player.Character)

				if hitObject then

					while true do
	wait(0.1)
	local target = FindPlayer(script.Parent.Torso.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
	end
end

				end

			end

		end

	end

	end
	
	end