script.Enabled = False problem

the problem with script.Enabled = false its that if the script is automatically running then it will stop i think? and im trying to reset it instead of blocking it

2 Likes

Want a workaround?

This might sound crazy but put all your code into a function and bind it to an attribute so that when it changes the code will re-run. You can change this attribute in other scripts making it easier too

2 Likes

Script reset:

  1. Disable the script
  2. Enable the script

So do:

local function resetScript(): ()
	--this works as intended for some reason
	--I expected to need a different thread for this
	script.Enabled = false
	script.Enabled = true
end

--when you want to reset your script
resetScript()
1 Like

So your question is a little vague.
Do you want the script that is running to continue until it’s finished, then stop? Then don’t disable/enable it. Make the code do that part of it.

What do you mean by reset it? Do you mean rerun the entire script?

Setting the .Enabled property to false then back to true will cause the script to re-execute entirely from the start.

1 Like

idk because i did a localscript that disabled another localscript on mousebutton1click but the other localscript didnt disabled right, basically after it wasnt working right

once you set Enabled property to “false” script will stop in middle of execution, and if you try to enable it back, script gonna run from first line of code again

ok so if i wanted to make the script end 100% of the time before disabling it what can i do?

add delay before disabling it, or remove wait() or task.wait() functions in script

There is also a script.Disabled property thats not normally visible, try script.Disabled = true

I mean like how long is the script? Is there something preventing you from using modules scripts? It’ll allow you to let the entire function run and still be able to recall that function several times again from other scripts. I feel like toggling script.Enabled isn’t general practice.

If you wouldn’t mind sharing what’s in the script we could possibly look into other alternatives would suite your needs while following standard practice.

i can’t think of a valid reason of doing this…

have you considered using a module script for rerunning code? you can bind an event to it whenever you want a script to “rerun” it

As | said in my first post, and others have said after that: don’t have 2 scripts.
I’m going to say script 1 for the new script below, and script 2 as the script you are trying to disable and enable to reset it.
Use functions in your new script 1.
You can start a function, set all your variables, etc. same as you do in script 2, run the exact same code as you’ve got in script 2, and then finish the function which completes script 2 without shutting it down too early.
Next time it’s needed you call the function from inside somewhere in script 1.

If the reason you are doing this is because script 2 has a while true do loop and you are just enabling/disabling it to stop the the loop from running then put an if check in that section of code to stop the loop. In this example I use the variable done that is set to true or false depending on some other section of code:

while not done then --repeats until done is true
    -- do your repeating code here
end
1 Like

this is the script:

local Players = game:GetService("Players")

-- Function to find the tool with the highest JP
local function getToolWithHighestJP(player)
	local bestTool = nil
	local highestJP = -1

	-- Iterate through the player's backpack
	for _, tool in ipairs(player.Backpack:GetChildren()) do
		-- Check if the tool has a JP value
		local jpValue = tool:FindFirstChild("JP")
		if jpValue and jpValue:IsA("NumberValue") then
			-- Compare JP values
			if jpValue.Value > highestJP then
				highestJP = jpValue.Value
				bestTool = tool
			end
		end
	end

	return bestTool
end

-- Function to equip the best tool on respawn
local function onPlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		-- Wait for a short time to ensure the avatar is properly loaded
		wait(2) 

		local bestTool = getToolWithHighestJP(player)

		if bestTool then
			-- Equip the tool from the backpack
			bestTool.Parent = character
		end
	end)
end

-- Connect the player added event to the function
Players.PlayerAdded:Connect(onPlayerAdded)

-- Handle players who are already in the game
for _, player in ipairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end

its a localscript running in starterplayerscripts

But why are you trying to disable and enable this script?
Many people have suggested ways to stop from having to do it.

How would a script enable itself if its off? :thinking:

I have the same question, but apparently it does it.

Update: I finally fixed the thing

Then please let people know how you fixed it and mark that post as the Solution.
This allows other people to find an answer if they have the same issue and are searching the forums for an answer.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.