Script.Disabled = true BUT it's still running?!

I don’t see the issue in the documentation…

It does say right at the end:
Note, the above code snippet cannot be used within the script itself. This is because once the script is disabled the thread will terminate.

It tells us that you cannot do it from the script itself. Is it not assumed that the thread of the script would carry on if it has been done within the script itself seeming as they are telling everyone it cannot be called within it.

Why does anyone actually need to disable a script inside itself, it running out of lines has the same effect…

In the majority of my Scripts, I use a BoolValue as a debouce feature but it can also be used as a disable feature :slight_smile:

local Enable = true

Button.MouseButton1Click:connect(function()
	if Enable then
		Enable = false
		--code
		wait(0.1)
		Enable = true
	end
end)

This prevent the function to be called thousands of times… You can also add the fact that it would disable for more than 0.1 second

local Enable = true

Button.MouseButton1Click:connect(function()
	if Enable then --This button works only if the Enable var is true
		Enable = false
		print("Enable")
		wait(0.1)
		Enable = true
	end
end)

DisableButton.MouseButton1Click:connect(function()
	if Enable then
		Enable = false
		print("Disabled!")
		wait(8)
		print("Enabled!")
		Enable = true
	end
end)
1 Like

I did some testing, disabling scripts does work perfectly fine but there is a catch to it…

  1. It only works if the disabling script is not in the same parent as where the script that is being disabled is.

  2. You cannot disable a script with itself.

That’s the result I got…try it out and see for yourself…

Edit: I just realised this reply is 1 year late, sorry lol

4 Likes

@RuizuKun_Dev If you are using while loops. They will not stop even if you disable the script. Client Sided or Server Sided. The reason for this is while loops kindof running forever.

Instead of using While Loops. I suggest using repeat loops if you want the script to stop running when a specific thing happens. Example, if a Bool Value sets to true. The repeat loop will stop and will move on to next line of code.

Example:

repeat
    wait()
until workspace.BoolValue.Value = true

print("The bool value in workspace has been set to true")

What this code does it makes something happen when a bool has been set to true.

Note that theres another way of detecting if a bool value has been set either true or false.

BoolValue:GetPropertyChangedSignal("Value"):Connect(function()
       if BoolValue.Value == true then
              -- Code here to make something happen
       end
end) 
4 Likes

Hi there, I’ve come across this too. the following code should fix your problem.

while wait(1) do
	print("running")
	if script.Disabled == true then
		break
	end
end
1 Like

Problem is. You’re disabling the script with its own script. And that won’t work. You’ll need to make a new script. Make that new script disable the script you want to disable.

I know why it does this.

When you try to disable a script when the script is running an infinite loop, the loop will never stop even if the code is disabled. What you will have to do to fix this is disconnect the script entirely.

Example :

local connection

connection = function()
    script.Disabled = true
	print(script.Disabled)
	while wait() do
		script.Disabled = true
		print(script.Disabled)
	end
end)

-- the 2 functions that connect / disconnect a code
connection:Connect()
connection:Disconnect()