How Can I Stop A Script From Continuing Running Code?

So, I have this script but it destroys the script. Now, when it destroys the script, code still keep running. Is there anyway to break the code?

local RS = game:GetService("RunService")

	if RS:IsStudio() then
		warn("Test")
		script:Destroy()
	end
print("Testing")
10 Likes

Not sure what you mean by break the code once a script runs it won’t run again unless your using a while loop or something like that. If you want to destroy the script you should probably reference it by its name for example.

Script = workspace.[script name]
Script:Destroy()

2 Likes

It does destroy. The only thing is when it’s destroyed, it still runs the code…

2 Likes

Try

script.Disabled = true
1 Like

Modulescripts cannot be disabled. Probably should’ve mentioned it was a ModuleScript.

If this is a module script then destroying it won’t break the code. Try stopping the code in the script that requires this.

You just need to return out of it.

if --isStudio...
    return

return exits out of the current scope. By calling it from the global scope of your script, you are preventing any further execution.

14 Likes

debounce is your friend

local rs = game:GetService("RunService")
local debounce = false

if rs:IsStudio() then
     debounce = true
     if debounce == false then
           warn("brotest")
     end
end
8 Likes

Perfect. Tysm!

I have a question; why are you using a second if statement to check if debounce equals false when debounce will never be false by this point?

Also, this will not halt execution of the code as it’ll exit the first statement after setting debounce to true, and continue with the rest of the script.

1 Like

its preventing anything inside of the if debounce == false statement to only be executed when the debounce is false, which it never is. therefore only when he is not in studio he will be warned "brotest". the point is to stop script to continue running code

1 Like

He’ll never be warned outside of Studio, as this code will only run in Studio.

4 Likes

the script is making it where, when hes in studio while the debounce is false it will warn him, but we never make the debounce be false, therefore its stopping the script from continuing running code…

As @ClockworkSquirrel mentioned, you’re using debounce incorrectly. It’s supposed to be like this:

local debounce = false

local function thing() -- example
    if debounce then return end
    debounce = true

    -- rest of the code
    debounce = false
end

See this article by roblox that futher explains debounce.
@Aiden_12114 since you marked it as the solution, you should see this too.

That being said, debounce isn’t even the solution for this. You don’t need an extra variable to stop the script when it’s going be the same value all the time anyway. Basically, you’re creating a useless variable, setting the value to always true, and then checking if it’s false.

This code

local debounce = false

if rs:IsStudio() then
     debounce = true
     if debounce == false then
           warn("brotest")
     end
end

can be refactored to this


Yea. An empty block. Both scripts will do the same exact thing. I’m trying to explain why the above post is not the solution. It just demonstrates misusage of debounce.

6 Likes

@Aiden_12114 the post I’m replying to, by ClockworkSquirrel, is the actual solution, for the reasons I explained in my other post. Would you mind switching the solution to that so people seeing this thread in the future aren’t mislead by the current solution?

6 Likes