script.Disabled = true
print(script.Disabled)
while wait() do
script.Disabled = true
print(script.Disabled)
end
https://i.gyazo.com/f9f6913d6a28673be82699f2b2c75309.mp4
I’m so confused, how do I fix this?
script.Disabled = true
print(script.Disabled)
while wait() do
script.Disabled = true
print(script.Disabled)
end
https://i.gyazo.com/f9f6913d6a28673be82699f2b2c75309.mp4
I’m so confused, how do I fix this?
Yeah, a disabled script will still run. That’s pretty much expected.
EDIT: Hmm…
This is incorrect as far as I know, though. Weird.
If Disabled is set to true whilst a script is running, the current thread will be terminated.
https://developer.roblox.com/api-reference/property/BaseScript/Disabled
@XAXA this isn’t what I expected to happen
Yeah, the wiki is just wrong (in that regard, at least).
Funny thing is, this will result in the string being printed infinitely:
So the wiki is right in that re-enabling a script will restart it. Disabling does nothing to the running thread, though, though.
I think this is being misinterpreted, or the behaviour is rather strange. One thing I’ve seen in many scripts is something like this in a part:
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
print(hit.Name..' touched the script!')
script.Disabled = true
wait(5)
script.Disabled = false
end
end)
This solves the infamous “oh no script running too fast” problem that people have.
I think connections are disconnected or something, but the current thread still runs
@OverHash @XAXA
It should’t be like that. It should stop regardless.
It’s deactivated!!
I don’t think the Wiki is wrong it doesn’t make sense, what’s the point is being able to Deactivate a script if it’s not gonna deactivate?
It shouldn’t run after a certain line but it still does.
Although there is a dirty workaround.
You’re right. Connections do get disconnected. I think the documentation is just unclear about the behavior.
I would expect when a script is disabled, it would stop immediately. Try filing a bug report or documentation request for clarification on this behaviour.
From my experimentation it appears that disabling the script will not terminate the current thread, instead it’ll continue until it is terminated.
To explain further, we’ll use the below example:
script.Disabled = true
while wait() do
print("test")
end
… which will infinitely print test to the output, since the thread never terminates.
To fully visualise this, we can use a second example.
-- first
spawn(function()
-- second
spawn(function()
-- third
while wait() do
print("third")
script.Disabled = true
end
end)
while wait(1) do
print("second")
end
end)
while wait(1) do
print("first")
end
In the above example, we have three threads (outlined by the comments first, second and third. The while loop disables the script inside the third thread, however since this thread never terminates, third
will continue to be outputted, whilst the other two threads are both terminated (and nothing will be outputted).
Seems like this should be clarified within the documentation, all threads besides the current are terminated.
Corrections and questions are appreciated.
The current behavior of the current thread continuing has been around for a long time. I recommend filing either a documentation request or bug report. It’s unclear which the intended behavior is, it could be either.
I copied & pasted your script it continues to print third indefinitely, am I misunderstanding something?
let me double check, will edit when done.
Edit: appears that is the case, logic issue in my demonstration. All threads are terminated except for the current thread, which is my the script wasn’t disabled in the second thread. The third thread didn’t terminate whilst the other two did. I’m going to apply corrections to the original demonstration. Thanks for pointing it out.
Edit2: updated original post.
I added some minor prints
-- first
spawn(function()
-- second
spawn(function()
-- third
while wait() do
print("third")
script.Disabled = true
end
end)
print('second')
wait(1/10)
print('waited')
script.Disabled = true
print(script.Disabled)
end)
wait(5); print("first")
Anything longer than wait(1/10) will not print “waited”
My hypothesis is it takes 1/20 seconds to disable the script
This is not the case. What is happening is that the script is being disabled before the code is ran (the script.Disabled = true inside the third thread terminates all the other threads except for the third thread).
The below example demonstrates this, the current thread isn’t terminated.
script.Disabled = true
while wait() do
print("not disabled")
end
which will infinitely output not disabled
.
You are right this code prints indefinitely
script.Disabled = true
wait(5)
while wait() do
print("not disabled")
end
Now I have no clue of what is going on, I discovered this by putting
script.Parent:Destroy()
script.Disabled = true
and then it still printed stuff which made me curious
Your code still prints third indefinitely, I don’t think it’s possible to terminate the thread.
however
-- first
spawn(function()
-- second
spawn(function()
-- third
while wait() do
print("third")
script.Disabled = true
break
end
end)
while wait(1) do
print("second")
end
end)
while wait(1) do
print("first")
end
inserting a break fixes it
output only printed third once.
This is because the third thread (current thread from the pov of disabling the script) isn’t terminated (since it contains a loop which infinitely runs), whilst the first and second thread are. Inserting a break
fixes this because it breaks the loop allowing the thread to finish.
Now I understand the situation slightly better thank you for the insight.
So is this suppose to be happening as an intended behavior and I should request more documentation or it is definitely a bug?
I can’t say for certain, as @Avigant stated there’s no reference as to whether this is a bug or an issue with documentation.
At the very least I’d recommend creating a request to have the behaviour documented, irregardless if it’s intended behaviour or not it can cause confusion.
I mean this might be the answer but I don’t know.
I think everyone is getting a little confused with what a thread is
Just because you’ve disabled the script does not mean you have terminated the current thread. In other words, it will still do whatever is in the code, however it will not repeat itself again and run through just once. I’m too tired to realize if this is the correct answer or not.
As for @XAXA and the infinite printing, that is also the same thing again.
You’ve disabled the script, that is fine. And then you just re-enabled the script which means it will run itself through again.
The script is thinking:
script disabled
oh she turned me off, damn… can’t run again.
guess i’ll print “this shouldn’t print lol. the wiki is wrong.” then die…
re-enables, so it runs again. basically does the same process again of thinking its been shut off.
Just because you disabled the script does not mean you have killed the current thread.
Also running:
script.Disabled = true
print("Hello World")
is the exact same as
print("Hello World")
The script will only run once either way.
In what situation could it run again besides being disabled? Scripts don’t infinitely loop after all.