--[[local function attack(target)
	local distance = (TeddyAI.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	if distance > 90 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		--stop movement
		--TeddyAI.Humanoid.WalkSpeed = 0
		--target.Humanoid.WalkSpeed = 0
		-- play sound
		--TeddyAI.Head.AttackSound:Play()
		--fire event
		local events = game.ReplicatedStorage:WaitForChild("Events")
		local player = game.Players:GetPlayerFromCharacter(target)
		events:WaitForChild("playerDeath"):FireClient(player, TeddyAI)
		-- play the animation
		local attackAnim = humanoid:LoadAnimation(script.AttackAnim)
		attackAnim:Play()
		attackAnim.Stopped:Wait()
		-- kill the player
		target.Humanoid.Health = 0
		--restart movement
		TeddyAI.Humanoid.WalkSpeed = script.Parent.Humanoid.WalkSpeed
	end
end]]


local function displayPath(waypoints)
	local color = BrickColor.Random()
	for index, waypoint in pairs(waypoints) do
		local part = Instance.new("Part")
		part.BrickColor = color
		part.Anchored = true
		part.CanCollide = false
		part.Size = Vector3.new(1,1,1)
		part.Position = waypoint.Position
		part.Parent = workspace
		local Debris = game:GetService("Debris")
		Debris:AddItem(part, 6)
	end
end

local function walkTo(destination)

	local path = getPath(destination)

	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				attack(target)
				break
			else
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(destination.Position - (TeddyAI.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

function patrol()
	local waypoints = TeddyAI.Parent.TeddyWaypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end

while true do
	patrol()
end
