Script is still running despite being disabled

I have a script that still runs even if it gets disabled by another script, I even implemented a check system to check if the script is enabled, but even if it’s disabled, it still runs!

Here is the script:

while wait(.5) do
	if script.Enabled == true then
	local children = game.Players:GetChildren()
	for i = 1, #children do
		
		if children[i].Character ~= nil then
		local hum = children[i].Character.Humanoid
					hum.JumpHeight = children[i].leaderstats.Jump.Value/2
		end
	end
	end
	end
2 Likes

fixed

while wait(.5) do
	if script.Disabled == false then
		local children = game.Players:GetChildren()
		for i = 1, #children do
			if children[i].Character then
				local hum = children[i].Character.Humanoid
				hum.JumpHeight = children[i].leaderstats.Jump.Value/2
			end
		end
	else if script.Disabled == true then
			break
		end
	end
end
2 Likes

script.Enabled hasn’t been enabled yet (no pun intended). It’s a new feature and will soon replace script.Disabled. in the UI to match with other Roblox instances. That’s likely why it wasn’t working, since script.Enabled doesn’t currently do anything.


I have some suggestions. For one, you probably don’t want to use a while loop here, since you’ll be running code forever. Really you only need to set JumpHeight once when the player’s character is first added, or when the Jump value changes.

It might simplify your code quite a lot to use events like PlayerAdded, CharacterAdded, and for your leaderstat, :GetPropertyChangedSignal("Value"). In turn, that might make it a lot easier to find what issues you’re running into.

This should additionally mean that Roblox will properly be able to stop your script, but, usually you want to avoid relying on that if you can.


To start with, you can make a function for when a player gets added, that way you can keep things organized. Then you can loop over Players:GetPlayers() and call it for all the players that might already be in the game when your code starts running, and then you can connect it to Players.PlayerAdded, so any new players also run that code.

local function playerAdded(player)
	-- Some code to run for each player
end

-- For all the players currently in the game
for _, player in ipairs(Players:GetPlayers()) do
	playerAdded(player)
end

-- For all the players who join
Players.PlayAdded:Connect(playerAdded)

Then, you can connect to player.CharacterAdded, so that you can run some code for every character that is added automatically. Lastly, you can update the JumpHeight inside there, and, if you’d like, you can connect to the Jump leaderstat changing outside of CharacterAdded, inside your playerAdded callback.


That should even let you do all the stuff you want without needing to hold on to the event connections and call :Disconnect() on them at all, which, is sometimes something that you want to keep in mind. For example, if the leaderstat wasn’t in the player, and was instead a value in ReplicatedStorage, Roblox wouldn’t delete it when the player leaves, so it would stay connected. That can start to cause lag, and will use a tiny bit of memory (called a memory leak since it uses a little memory even though the player or thing it was for is gone) so sometimes you also want to add the result of Connect to a variable and then call :Disconnect() when you know you don’t need it anymore.

When an instance gets deleted (e.g. via :Destroy() or via a Player leaving the game), all the connections on any events from that instance will also get cleaned up automatically by Roblox.


@round_63 Pasting fixed code without explaining how it fixes the issue isn’t very helpful to the person asking for help, it doesn’t help them to learn what went wrong, and generally it’s my understanding that you should try and avoid that.

2 Likes

i recommend using pairs on the for loop

2 Likes

Well, the reason the script runs with a while loop is because every second, the player is given a “Jump” stat, and bascially the whole game idea is that every second they gain +1 Jump height, also I am going to try the script.Disabled thing, hopefully it works

This, unfortunately did not work. I’m not sure why, but it still does the same issue as stated before…

Mhm. That makes sense. I almost never use while loops for anything now (e.g. while task.wait(0.5) do) since eventually, having lots of those starts to add up to a lot. It also makes the code less easy to read.

Usually I’ve seen it referred to as “polling” to check values in a loop to run a value (that’s also what I’d call it), and, it can lead to problems. By using events, not only do you get your code to work instantly you only ever run code exactly when it’s needed. It sometimes feels like more work at first, but, over time I learned that it usually actually saves me time.

It also lets you avoid using task.wait (or the older wait) altogether. A lot of the time, using wait a lot means that your code can randomly seem to break for no reason at all. For example, maybe you call wait() before you grab an instance inside a LocalScript. But, maybe a player connects and it takes Roblox a little bit longer than normal to send that instance to them. Then, suddenly the code throws an error because the instance doesn’t exist yet.

That’s referred to as a race condition (because Roblox and your code are racing, if Roblox doesn’t win and make that instance before your code runs, your code throws an error since that instance doesn’t exist, and, it can result in a lot of confusion). Often times, as a solution, people then start adding bigger waits, making their code take longer. But, really, you could just use an event, or use WaitForChild (which is sort of like calling instance.ChildAdded:Wait() in a loop and checking FindFirstChild) and that will always work, no matter what, as fast as it possibly can.

Plus, that also works if you enable StreamingEnabled too.

1 Like

I appreciate the time you are spending writing this, but either I don’t understand what any of this means, or it is not relevant to my problem, I must remind you that the issue here is that the script is still running despite it being disabled by another script.

I am not meaning to be rude or ignorant by saying this by the way.

2 Likes

i have been trying your code for so long and i didnt manage to get it right

1 Like

Do you think it would help if I told you it is a server script located in StarterCharacterScripts, and the script which is disabling it is a LocalScript inside a Text Button in StarterGUI

that makes me more curious
right now im still trying different ways

1 Like

Oh, ok, thanks for going this far to help me with this, I really do appreciate it.

it seems like the problem is that when the script was disabled, it wont do any conditional tests

i have tried using an if condition and even went to the repeat loop
im gonna try values

okay i have tried values, it works
code:

repeat
	for _, player in pairs(game.Players:GetChildren()) do
		if player.Character then
			local hum = player.Character.Humanoid
			hum.JumpHeight = player.leaderstats.Jump.Value / 2
		end
	end
	wait(0.5)
until script.Run.Value == false

explorer:
image
properties:
image
on the local script if you want to disable it set run’s value to false

1 Like

Even though the Run value is set to false, the script is still running

you can convert the server script above to a local script and setting the things to local player

1 Like

Ohhh this works by having it as local script, thank you so much this is actually so helpful!

local Jump = nil --Define jump.
local Run = nil --Define run.

local function OnJumpChanged(JumpValue)
	Humanoid.JumpHeight = JumpValue / 2
end

local function OnRunChanged(RunValue)
	--Code.
end

Jump.Changed:Connect(OnJumpChanged)
Run.Changed:Connect(OnRunChanged)

Use the Changed event/signals of value objects as recommended by the official documentation.

2 Likes