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