Hello! So I am making a FNAF game on Roblox and I have a power system on it. The power never goes out since the power goes to the negative (because the powerusage) So for example if the power was 2 and all doors were opened it would go to -2. Is it possible for it to stop at 0 and not go under 0?
script:
local Player = game.Players.LocalPlayer
local Power = game.ReplicatedStorage.Power
local PowerUsage = game.ReplicatedStorage.PowerUsage
game.ReplicatedStorage.NightStart.OnServerEvent:Connect(function()
while wait(5 - PowerUsage.Value) do
Power.Value -= PowerUsage.Value
end
end)
local PowerValue = game.ReplicatedStorage.Power
local Debounce = false
while wait(0.1) do
game.Workspace.Power.SurfaceGui.PowerText.Text = "Power ".. PowerValue.Value.. "%"
if Power.Value == 0 or Power.Value == -1 or Power.Value == -2 or Power.Value == -3 then
if Debounce2 == false then
Debounce2 = true
game.Workspace.DoorButton:Destroy()
game.Workspace.LightButton:Destroy()
game.Workspace.DoorButton2:Destroy()
game.Workspace.LightButton2:Destroy()
game.Workspace.Door:Destroy()
game.Workspace.Door2:Destroy()
game.Workspace.Power:Destroy()
game.Workspace.Sounds["FNaF 1 Door Light [Loop]"]:Stop()
game.Workspace.Sounds["FNaF 1 Door Light [Loop]2"]:Stop()
wait(1)
script:Destroy()
end
end
end
You could just use Power.Value <= 0 and that would detect if it ever is at or below 0. After that, just set the value to 0 and set your Text to “Power 0%”.
Also, note of concern; This is highly exploitable. Since this is a local script, an exploiter could forever set the value of Power to 100, and so, they would never get a black out.
What do you mean by that? You have both local Player = game.Players.LocalPlayer and OnServerEvent, which are exclusive to LocalScripts and Client-Sided ModuleScripts, which doesn’t change much.
It shouldn’t stay frozen though. Did you copy the script after I added the task.wait()? here’s what it should look like in the actual script:
local Power = game.ReplicatedStorage.Power
local PowerUsage = game.ReplicatedStorage.PowerUsage
game.ReplicatedStorage.NightStart.OnServerEvent:Connect(function()
while Power.Value > 0 do
Power.Value -= Power.Value >= PowerUsage.Value and PowerUsage.Value or Power.Value
task.wait(5 - PowerUsage.Value)
end
end)
local Debounce = false
while wait(0.1) do
game.Workspace.Power.SurfaceGui.PowerText.Text = "Power ".. Power.Value.. "%"
if Power.Value <= 0 then
if Debounce2 == false then
Debounce2 = true
game.Workspace.DoorButton:Destroy()
game.Workspace.LightButton:Destroy()
game.Workspace.DoorButton2:Destroy()
game.Workspace.LightButton2:Destroy()
game.Workspace.Door:Destroy()
game.Workspace.Door2:Destroy()
game.Workspace.Power:Destroy()
game.Workspace.Sounds["FNaF 1 Door Light [Loop]"]:Stop()
game.Workspace.Sounds["FNaF 1 Door Light [Loop]2"]:Stop()
wait(1)
script:Destroy()
end
end
end
math.max returns the largest value among the numbers passed into the function. It is helpful for establishing lower boundaries for your values. In your case, it will prevent Power.Value from going below zero.
You could apply it by replacing Power.Value -= PowerUsage.Value with Power.Value = math.max(Power.Value - PowerUsage.Value, 0)