While loop continuing despite script being disabled

Hey! I am trying to disable the script temporarily and the while loop is still going. I tried breaking it, but then it broke the game functions. Is this intentional or a bug of some sort?
Code if needed:

local ServerStorage = game:GetService("ServerStorage")
local PowerService = ServerStorage:FindFirstChild("Power")

while task.wait(0.5) do
	workspace.Power.Button.Color = Color3.fromRGB(0,0,0)
	PowerService.CurrentPower.Value -= PowerService.PowerBeingUsed.Value/10
	if PowerService.CurrentPower.Value >= PowerService.MaxPower.Value/2 then
		workspace.Power.Shock.Transparency = 0
	else
		workspace.Power.Shock.Transparency = 0.7
	end
	if PowerService.CurrentPower.Value <=0 then
		for index, value in pairs(game:GetService("Players"):GetPlayers()) do
			local CheckForCharacter = value.Character or value.CharacterAdded:Wait()
			local Character = value.Character
			value.Character:WaitForChild("ShowPowerStationRestore").Disabled = false
		end
		print("Hey!")
		game.ServerStorage.Power.Gui.CanMessageOff.Value = true
		PowerService.PowerBeingUsed.Value = 0
		workspace.Power.Button.BillboardGui.Enabled = true
		workspace.Power.Shock.DamageScript.Disabled = true
		workspace.Power.Shock.Transparency = 1
		workspace.Power.Shock.CanCollide = false
		script.Disabled = true
	end
end

IIRC there’s currently a bug with task.wait where it continues running the script even if it’s disabled. Try replacing it with wait().

Still continues. No difference.

script.Disabled = true

This will not stop the loop for a while, consider adding a condition if true or false.

Try this:

local ServerStorage = game:GetService("ServerStorage")
local PowerService = ServerStorage:FindFirstChild("Power")

while task.wait(0.5) do
    if script.Disabled then
        return
    end
	workspace.Power.Button.Color = Color3.fromRGB(0,0,0)
	PowerService.CurrentPower.Value -= PowerService.PowerBeingUsed.Value/10
	if PowerService.CurrentPower.Value >= PowerService.MaxPower.Value/2 then
		workspace.Power.Shock.Transparency = 0
	else
		workspace.Power.Shock.Transparency = 0.7
	end
	if PowerService.CurrentPower.Value <=0 then
		for index, value in pairs(game:GetService("Players"):GetPlayers()) do
			local CheckForCharacter = value.Character or value.CharacterAdded:Wait()
			local Character = value.Character
			value.Character:WaitForChild("ShowPowerStationRestore").Disabled = false
		end
		print("Hey!")
		game.ServerStorage.Power.Gui.CanMessageOff.Value = true
		PowerService.PowerBeingUsed.Value = 0
		workspace.Power.Button.BillboardGui.Enabled = true
		workspace.Power.Shock.DamageScript.Disabled = true
		workspace.Power.Shock.Transparency = 1
		workspace.Power.Shock.CanCollide = false
		script.Disabled = true
	end
end

Maybe you could return something? eg. return end.

try adding a break onto it, see if that works

You all can’t find a simple solution, because your doin while task.wait do, which continuously does the loop Everytime the wait hits ignoring the script.disabled. try doing while true do with a task.wait.

there’s 2 workarounds

  1. Do while true do with a task.wait inside it
  2. Use the wait() instead

for some reason task.wait hates script.disabled

task.wait() would still run even though you disabled the script. This is a bug and it’s been reported:

For now, just use the legacy wait() function.

When scripts are destroyed or disabled, any currently running loops will still remain running. This is why scripts can run without a source. If you do not wish for this behaviour change your loop to something such as

repeat
	--Code here
	task.wait(.5)
until script.Disabled or not script.Parent

I mentioned if I use break, it malfunctions.