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.
In the majority of my Scripts, I use a BoolValue as a debouce feature but it can also be used as a disable feature
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)
@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)
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.
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()