Yea, but apparently that’s not the case when you disable the script from a different script.
That is because the other script can stop anymore threads being created, whereas doing it internally means it stops every other thread except itself.
Until the thread has got to the end of the script, it will keep running. The other script cannot however disable any current threads which are active.
I’m not sure what you mean. What I’m saying is the script can only be stopped with Disabled
if it’s set from another script
Consider the following code:
_G.me = script
while wait() do
print(math.random())
end
Now, doing _G.me.Disabled = true
from another script is going to stop the output spam.
However, doing this
while wait() do
print(math.random())
script.Disabled = true
end
will not stop the loop.
What I said previously was wrong, the outer thread can stop scripts and the current thread running.
When it is done inside the script, it doesn’t change the current threads properties - that is because it is a thread, an instance of the script. It doesn’t understand that it can be ‘disabled’. If it is done outside the script then the script can affect the current running thread (instance) of the script, as such it can disable it.
Threading is quite hard to explain, so apologies if any of this causes blanks. The point is however, that whereas the thread cannot change itself - outer scripts can.
Let’s take a step back here and address the why. What prompted this thread to be made? Are you testing out properties in Studio or do you have a specific use case in mind where this is applicable?
A thread of a script is made when any script is ran, this is a general issue meaning that it cannot be generalised into a single script causing this ‘issue’. Every script is bound by this ‘issue’ of disabling.
I’ve put ‘ around issue because it’s not really an issue, it’s just people not understanding.
I don’t want this to sound rude, but can we stop making this chat thread more complicated as we are over complicating a simple issue - expanding an issue is fine if you don’t understand or need something explained.
So you are saying the problem is solved if I Disable it from another script?
Any other script even a module?
Edit;
(This is not possible, if the script disabling itself requires the module)
Although Disabling a script with another script will deactivate the script immediately.
This has happened to me as well… Can’t remember the specific case though.
By thread, I wasn’t referring to a script. I was talking about this DevForum thread. I think you misconstrued the meaning of my response.
I don’t care to make this more complicated. I’m asking about the root issue as to why the Disabled property needs to be used in the first place.
Please read my post again.
The script will not be disabled until the script finishes running, for example, never ending loop will never end even if script is disabled.
script.Disabled = true
while wait() do
print('a')
end
do this then
local disabled = script.Disabled
script.Changed:Connect(function(prop)
if (prop == "Disabled") then
disabled = script.Disabled
end
end)
while wait() do
if (disabled) then
break
end
print("1")
end
Work-arounds such as the one you mentioned in your post shouldn’t be relied on (not targeting what you posted specifically, however the concept of it), in general they’re messy and realistically there isn’t a point in having to implement them since there shouldn’t be an instance where disabling a script after run-time is the best solution as mentioned by @colbert2677.
Irregardless, according to the developer.roblox.com documentation, all threads should be terminated, however this doesn’t accurately reflect the current behaviour where the current running thread will continue until it finishes. The documentation should be updated accordingly to reflect this or the behaviour should be changed to reflect the documentation.
Apologies, I have been talking about Threading in this thread so if you can’t tell it’s getting VERY CONFUSING at this point.
The main question was basically answered / investigated, now it is just people asking addition questions.
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
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)
I did some testing, disabling scripts does work perfectly fine but there is a catch to it…
-
It only works if the disabling script is not in the same parent as where the script that is being disabled is.
-
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
@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)
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
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()