[HELP] Citizen is instantly detecting you after becoming undetectable

This is my code for a detection system:

local LookPart = script.Parent.Head
local func = game.ServerStorage.Bindables.Alarm

local debounce = false

while task.wait() do
	for _, Player in pairs(game.Players:GetPlayers()) do
		Character = Player.Character or Player.CharacterAdded:Wait()
		local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
		local Dot = (LookPart.Position - HumanoidRootPart.Position).Unit:Dot(LookPart.CFrame.LookVector)
		if Dot > 0.7 then
			local Params = RaycastParams.new()
			Params.FilterType = Enum.RaycastFilterType.Exclude
			Params:AddToFilter({LookPart,Character:GetChildren()})
			local Raycast = workspace:Raycast(LookPart.Position,-(LookPart.Position - HumanoidRootPart.Position).Unit * (LookPart.Position - HumanoidRootPart.Position).Magnitude,Params)
			if not Raycast then
				if game.Players:FindFirstChild(Character.Name) then
					local value = game.Players:FindFirstChild(Character.Name).Values.Detectable
					if script.Parent.Humanoid.Health ~= 0 then
						if value.Value == true then
							if debounce == false then
							debounce = true
								local i = 5
								
								repeat 
									if value.Value == true then
								task.wait(1)
								i -= 1
										print(i)
									else
										print("ok")
										
										break
									end
							until i == 0
							local reason = " got spotted by a citizen and raised the alarm!"
								local plr = game.Players:FindFirstChild(Character.Name)
					
							func:Fire(plr, reason)
									debounce = false
								
							
							end
						end
					end
				end
				local char = Character
				local player = game.Players[char.Name]
			end
		end
	end
end

Here, where main detection is managed, if you stop being detectable in front of the NPC, the event immediately fires and sounds the alarm, instead of stopping detection, how can I fix this? (SORRY FOR WEIRD FORMATTING)

						        repeat 
									if value.Value == true then
										task.wait(1)
										i -= 1
										print(i)
									else
										print("ok")
										break
									end
								until i == 0
								
								local reason = " got spotted by a citizen and raised the alarm!"
								local plr = game.Players:FindFirstChild(Character.Name)
					
								func:Fire(plr, reason)
								debounce = false
																

response to deleted post…

Can you try uploading the video to a video service? If not,
Try changing Exclude to Blacklist. I know its deprecated, but it worked better and broke half of my code when I used Exclude.

That’s not working, it still instantly detects. I’ve narrowed the issue down to needing a way to stop the code after checking a value, I don’t know a way to instantly stop any code, so if you know it would be helpful

Stopped code by adding return after checks are false

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