Script doesn't work second time

I took this script of youtube and edited it to my own needs which is when a player sees a humanoid it registers his HumanoidRootPart into an ObjectValue and then calculates a path to it, it all works but second time the dummy just freezes mid way to the player and im not sure why.

function checkSight(target)
    local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 20)
    local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {script.Parent})
    if hit then
        if hit:IsDescendantOf(target.Parent) and math.abs(hit.Position.Y - myRoot.Position.Y) < 3 then
            return true
        end
    end
    return false
end

function findPath(target)
    local path = game:GetService("PathfindingService"):CreatePath()
    path:ComputeAsync(myRoot.Position,target.Position)
    local waypoints = path:GetWaypoints()
    
    if path.Status == Enum.PathStatus.Success then
        for _, waypoint in ipairs(waypoints) do
            if waypoint.Action == Enum.PathWaypointAction.Jump then
                myHuman.Jump = true
            end
            myHuman:MoveTo(waypoint.Position)
            local timeOut = myHuman.MoveToFinished:Wait(0.5)
            if not timeOut then
                myHuman.Jump = true
                findPath(target)
                break
            end
            if checkSight(target) then
                repeat
                    myHuman:MoveTo(target.Position)
                    attack(target)
                    wait()
                    if target == nil then
                        break
                    elseif target.Parent == nil then
                        break
                    end
                until checkSight(target) == false or myHuman.Health < 1 or target.Parent.Humanoid.Health < 1
                break
            end
            if (myRoot.Position - waypoints[1].Position).magnitude > 20 then
                findPath(target)
                break
            end
        end
    end
end
 
function findTarget()
	local target = game.workspace[PlayerName].HumanoidRootPart
	print(target.Parent.Name)
	return target
end
 
function attack(target)
    if (myRoot.Position - target.Position).magnitude < 5 then
        if target.Parent ~= nil then
            target.Parent.Humanoid:TakeDamage(100)
        end
        wait(0.1)
    end
end


function main()
    local target = findTarget()
    if target then
        myHuman.WalkSpeed = 29
        findPath(target)
    else
        myHuman.WalkSpeed = 8
	    end
end

while true do
	
	
	local beingWatched = false
	local playerAttack = {
		player = nil,
		playerDirection = nil,
		playerMagnitude = 100
	}
	
	for _, player in pairs(players:GetPlayers()) do
		--if beingWatched then break end
		
		-- check to see if the SCP is being looked at
		if player and player.Character and player.Character:FindFirstChild('Humanoid') and player.Character.Humanoid.Health > 0 and player.Character:FindFirstChild('Head') and player.Character:FindFirstChild('HumanoidRootPart') then
			if playerCanSeeSCP(player, false) then
				beingWatched = true
				local Victim = Instance.new("ObjectValue")
				Victim.Value = player
				Victim.Name = player.Name
				Victim.Parent = Victims
				PlayerName = player.Name
				script.Parent.Configuration.Triggered.Value = true

			else
				local sub = (script.Parent.UpperTorso.CFrame.p - player.Character.Head.CFrame.p)
				if sub.magnitude < playerAttack.playerMagnitude then
					playerAttack.player = player
					playerAttack.playerDirection = sub.unit
					playerAttack.playerMagnitude = sub.magnitude
				end
			end
		end	
	end
	
	if  beingWatched and playerAttack.playerMagnitude < 200 then

		Idle:Stop()
		Triggered:Play()
		Triggered.Stopped:wait()
		WindUp:Play()
		wait(10)
		WindUp:Stop()
		Rage:Play()
		main()
		if game.workspace[PlayerName].Humanoid.Health == 0 then
			script.Parent.Configuration.Triggered.Value = false
			PlayerName = nil
		end
		Rage:Stop()
		Idle:Play()
		Victims:ClearAllChildren()
		wait(7)
end
wait()
end

I know It’s a lot to read but most of it I don’t think is broken.

I would like to start by saying using other scripts will blind you from seeing outstanding issues; writing your own scripts will help you understand issues better.

Aside from that, we need more information. What does the output show?

Nothing it just randomly stops mid way on second try

Ok, let’s debug it then :wink:

Put some random prints in relevant places, and check the output. When does it not print?

Looks like the checkSight is not running 2nd time

That means it’s not being called due to something beforehand yeilding, infinitely repeating, or throwing an error. Check the parent function for an issue like this.

I have the same problem! how to do fix IDK