How would I get this value to stop going to the negatives?

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
2 Likes

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.

1 Like

this is a script not really a local script

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.

while Power.Value > 0 do
	Power.Value -= Power.Value >= PowerUsage.Value and PowerUsage.Value or Power.Value
	task.wait(5 - PowerUsage.Value)
end

Also yes, remove the Player variable, LocalPlayer doesn’t work on ServerScripts and is just nil

1 Like

i dont use the player function so i removed that but for some reason onserverevent works on scripts

1 Like

My bad, OnServerEvent is server exclusive I just had a dumb moment

oh one more thing! It didnt work for some reason. It still shows as -1 and the power doesnt go out

Could you use my version of the while loop?

I added it and the power stays frozen?

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
1 Like

You meant this?

while Power.Value >= 0 do
	Power.Value -= Power.Value >= PowerUsage.Value and PowerUsage.Value or Power.Value
	task.wait(5 - PowerUsage.Value)
end

If I added the >= it would be an infinite loop.

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)

use math.clamp whenever you set the value

You can clamp the Power’s value:

Power:GetAttributeChangedSignal("Value"):Connect(function()
    Power.Value = math.clamp(Power.Value,0,math.huge)
end)

Every time the value gets changed and it’s lower than 0, it will become 0.

"GetAttributeChangeSignal is not a valid member of NumberValue “ReplicatedStorage.Power”

It should work now, I had made a typo