How to disable a running script?

So I’m working on a new game and i made a gui button to disable an other running script, by clicking it.
it marks the script as disabled, but the script won’t stop. i need a urgent help please.

here’s the code
(Keep in mind i’m not a scripting professional i’m just a noob so yea don’t judge me about the bad coding skills ;()

local ScreenGui = script.Parent

local TextButton = ScreenGui.TextButton

TextButton.MouseButton1Up:Connect(function()

game.StarterGui.Main.LocalScript.Disabled = true

wait()

end
2 Likes

I added the end) it still won’t work. idk why.

Instead of disabling it on the starter gui, just disable it on the playergui (not sure but its worth a try |:)

2 Likes

I’m not sure what you mean by that :confused:

Is main a gui??? 30charrrrrrrrr

I don’t know why you are making a new thread when there is one to explain your question. Script.Disabled = true BUT it's still running?!

I don’t know, maybe I haven’t seen this?

I don’t know. Maybe you should try look for the answer? Is it that hard to search up your problem into your bowser that you have to make a whole new thread? Only if you can’t find the answer anywhere then you should make a thread but there is no reason to make a post when there are plenty of answers on the internet…

If you come out to answer my question it’s fine but don’t answer if you’re not about to help,

is the script parented to gui?

Im helping you because i’m redirecting you to a thread that had been made to answer your question. Just because you are lazy and can’t look up your issue on the internet its not my problem.

This is the correct solution.

game.StarterGui is only a source container from which items will be cloned locally into game.Players.LocalPlayer.PlayerGui

Replace your line

game.StarterGui.Main.LocalScript.Disabled = true

with

game.Players.LocalPlayer.PlayerGui.Main.LocalScript.Disabled = true

You’d also want to make sure that ResetOnSpawn is set to false on the Main gui so it won’t reset when the player respawns, unless that’s desired behaviour.

Reference: StarterGui | Documentation - Roblox Creator Hub

4 Likes

If you want to do it for everyone then use a for loop

1 Like

Thank you, it works, finally a positive solution for this.

Avoid disabling scripts if you can. There is always going to be a better option available to you that involves properly cleaning up your code and objects. Disabled should scarcely or never be used in production code. What kind of script are you trying to disable?

1 Like

I’m making a lag test place, and if you click that button that i mentioned, it disables the script from generating parts.

Are you not able to add a condition or event of some kind instead that tells the script to change a boolean variable in your generator script? Assuming that you’re using a loop of some kind to generate parts, you can read this boolean for your condition and stop the loop if it’s true (or some other thing you expect).

Imagine this scenario: in a while loop, the body of the loop generates a single part.

local haltGeneration = true

-- Assume a BindableEvent exists somewhere
ExternalHaltGeneration.Event:Connect(function ()
    haltGeneration = true
end)

local function startGeneration()
    haltGeneration = false

    while not haltGeneration do
        -- Generate one part
    end
end

Then from your button script, you can call Fire on that ExternalHaltGeneration event to flip the condition which in turn stops generation. No involvement of Disabled plus the benefit of an event-based approach. Using break, by the way, also works, especially in for loops.

2 Likes

someone already solved my problem thanks for the help, but the other issue i’m facing is
I made a script inside a “reset” button which supposed to remove All parts named “lags” that have been found in workspace when someone clicks it, but it removes one by one each time you click, and not everything, Is there a solution for this? ;(

here’s the script that i made for that “reset” button

local ScreenGui = script.Parent
local TextButton = ScreenGui.TextButton

TextButton.MouseButton1Up:Connect(function()
workspace.lag:Destroy()
end)

Here’s a link of what i mean in a video https://twitter.com/RolingBlox/status/1257603750186881026?s=20