Robot character doesn't find payer when he's supposed to

I want to make the robot find the player but I have problems with doing so

whenever I enter the room and he is doing his searching animation he doesn’t spot me when but when I enter before he does his searching animation he spots me here is video I was in a bit of a hurry so I recorded it in Roblox recorder:
robloxapp-20240414-1501104.wmv (1.5 MB)

I tried fixing it with the limited knowledge I have I’m still new

here is script:

-- finding player this ones obvius
	local function findTrget()
		local players = game.Players:GetPlayers()
		local maxDistnace = 40
		local nearestTarget

		for index, player in pairs(players) do
			if player.Character then
				local target = player.Character
				local distance = (newChracter.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

				if distance < maxDistnace and canSeeTarget(target) then
					nearestTarget = target
					maxDistnace = distance
				end
			end
		end
		return nearestTarget
	end


-- function for attacking player this ones also obvius
	local function attack(target)
		local distance = (newChracter.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude


		if distance > 8 then
			track2:Stop()
			
-- random code that has nothing to  do with my problem it only closes the door until player dies
			tweenClose:Play()
			prompt.ActionText = "You Cant Escape Until You Die"
			
-- same with this one
			redtweenClose:Play()
			Redprompt.ActionText = "You Cant Escape Until You Die"
			
-- this is the important part of the script
			if not runningTrack.IsPlaying then
				runningTrack:Play()
			end
			humanoid:MoveTo(target.HumanoidRootPart.Position)
		else
			local PlayerDeath = game.ReplicatedStorage:WaitForChild("playerDeath")
			local player = game.Players:GetPlayerFromCharacter(target)
			PlayerDeath:FireClient(player)
			runningTrack:Stop()
			JumpscareTrack:Play()
			JumpscareSound:Play()
			JumpscareTrack:AdjustSpeed(2)
			JumpscareTrack.Stopped:Wait()
			target.Humanoid.Health = 0
		end
	end
	
	
-- this is where the functions are being used i think the problem is here but i dont know how to fix it
	local target = findTrget()


	VentSound:Play()
	VentSound.Ended:Wait()
	track:AdjustSpeed(1)
	track.Stopped:Wait()
	track2:Play()


	if target then
		
		print(target.Name)
		while task.wait(0.5) do
			attack(target)
			if target.Humanoid.Health == 0 then
				newChracter:Destroy()
				break
			end
		end
	else
		track2.Stopped:Wait()
		track:Play(0.100000001, 1, -1)
		track.Stopped:Wait()
			newChracter:Destroy()
		end	
	end

sorry if I couldn’t explain very well English is not my first language

I don’t see anything that loops the code you posted.

As it is, the code will only run one time.

I tried putting it in the same “while loop” as the “attack function” with a bit of tweaking but still got the same results

while task.wait(0.3) do
		local target = findTarget()
		if target and target.Humanoid.Health > 0 then
			attack(target)
			print(target.Name)
			
			if target.Humanoid.Health == 0 then
				newChracter:Destroy()
				break
			end
			
		else
			track2.Stopped:Wait()
			track:Play(0.100000001, 1, -1)
			track.Stopped:Wait()
			newChracter:Destroy()
			break
		end	
	end
end

it still gave me the same results

I’ve been trying to get it to work for weeks or even a month I finally got it to work! thanks btw :grin:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.