DetectLevel is a NumberValue in a folder inside the Player. Whenever i is updated, DetectLevel does not update with it.
Code snippet: (I apologize for the lack of indentation)
local i = 5
local detectlevel = Player.Values.DetectLevel
detectlevel = i
local ok = true
repeat
if value.Value == true then
ok = true
print(i)
i -= 1
detectlevel = i
task.wait(1)
else
print("not ok")
ok = false
debounce = false
detectlevel = 5
break
end
until i == 0 or ok == false
if ok == true then
local reason = " got spotted by a citizen and raised the alarm!"
local plr = game.Players:FindFirstChild(Character.Name)
func:Fire(plr, reason) -- func is a BindableEvent
debounce = false
end
I bet money you’re getting errors because you’re not getting the Value of DetectLevel.
Whenever you run code along the lines of detectlevel = x, what you’re doing right now is trying to set Player.Values.DetectLevel equal to x instead of Player.Values.DetectLevel.Value.
Check your output to see if you are getting an error before you try anything else. I don’t know if you’re getting an error or not, but you will get an error if you don’t fix what I mentioned
local LookPart = script.Parent.Detector
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
local detectlevel = Player.Values.DetectLevel
detectlevel = i
local ok = true
repeat
if value.Value == true then
ok = true
print(i)
i -= 1
detectlevel.Value = i
task.wait(1)
else
print("not ok")
ok = false
debounce = false
detectlevel.Value = 5
break
end
until i == 0 or ok == false
if ok == true then
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
end
local char = Character
local player = game.Players[char.Name]
end
end
end
end
local i = 5
local detectlevel = Player.Values.DetectLevel
detectlevel = i
Change it to:
local i = 5
local detectlevel = Player.Values.DetectLevel
detectlevel.Value = i -- Important! Set the *value* of DetectLevel to i, not DetectLevel itself