Why doesnt this loop stop when value changes

  1. What do you want to achieve? A script where the energy value decreases by 2 every 0.1 seconds while the button is held

  2. What is the issue? the script doesn’t stop after the value changes to off

  3. What solutions have you tried so far? I tried doing while loops, repeat loops and I tried combining them but nothing has worked so far

here’s the script that doesn’t work(server)

	while value == "LeftOn" do
		repeat 
			plrcharacter:FindFirstChild("Energy").Value -= 2
			wait(0.1)
		until value == "LeftOff"
	end

here’s the entire script(server)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage.Daisy.Quake

local quaking = false

local leftQuake

local leftDebounces = {}
local leftConnection

event.OnServerEvent:Connect(function(player, value)
	local plrcharacter = player.Character
		
	if value == "LeftOn" then
		quaking	= true
		plrcharacter:FindFirstChild("CanAttack").Value = false
		plrcharacter:FindFirstChild("CanRegen").Value = false
		leftQuake = ReplicatedStorage.Daisy.LeftQuake:Clone()
		leftQuake.CanCollide = false
				
		wait(0.5)
		
		leftQuake.CFrame = plrcharacter.LeftHand.CFrame * CFrame.new(0,-8,0)
		leftQuake.Parent = workspace
		for i, v in pairs(plrcharacter.LeftHand:GetDescendants()) do
			if v:IsA("ParticleEmitter") then
				v.Enabled = true
			end
		end
		
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = plrcharacter.LeftHand
		weld.Part1 = leftQuake
		weld.Parent = leftQuake
		
		leftConnection = leftQuake.Touched:Connect(function(target)
			local character = target.Parent
			if not character then return end

			local humanoid = character:FindFirstChild("Humanoid")
			if humanoid then

				if table.find(leftDebounces, character) then return end 

				table.insert(leftDebounces, character)
				
				local targetPlayer = game.Players:GetPlayerFromCharacter(character)

				local bv = Instance.new("BodyVelocity")
				bv.Velocity = CFrame.new(plrcharacter.HumanoidRootPart.Position, character.UpperTorso.Position).LookVector * 100 + CFrame.new(plrcharacter.HumanoidRootPart.Position, character.UpperTorso.Position).UpVector * -5
				bv.Parent = character.UpperTorso
				
				game.ReplicatedStorage.CamEvents.CamShake:FireClient(targetPlayer, "Shake", "Explosion")

				character:FindFirstChild("CanAttack").Value = false

				wait()

				character:WaitForChild("RagdollR15"):FindFirstChild("Activate").Value = true

				wait(0.25)

				bv:Destroy()

				for i = 1,25 do
					character.Humanoid:TakeDamage(1)
					wait(0.1)
				end

				wait(3)

				character:WaitForChild("RagdollR15"):FindFirstChild("Activate").Value = false
				character:FindFirstChild("CanAttack").Value = true
				
				if character.UpperTorso:FindFirstChild("BodyVelocity") then
					character.UpperTorso:FindFirstChild("BodyVelocity"):Destroy()
				end
				
				table.clear(leftDebounces, character)
			end
		end)
	end
	
	if value == "LeftOff" then
		for i, v in pairs(plrcharacter.LeftHand:GetDescendants()) do
			if v:IsA("ParticleEmitter") then
				v.Enabled = false
			end
		end
		quaking = false
		leftQuake:Destroy()
		plrcharacter:FindFirstChild("CanAttack").Value = true
		plrcharacter:FindFirstChild("CanRegen").Value = true
	end
	
	while value == "LeftOn" do
		repeat 
			plrcharacter:FindFirstChild("Energy").Value -= 2
			wait(0.1)
		until value == "LeftOff"
	end
	
end)

When firing the event, you are passing a value, that becomes a separate parameter, separate variable from the original one. So no matter how you change the original value, if you dont fire the remote again with changed value, for this script it always stays the same

1 Like

As a workaround, you can instead of a regular variable make a string value in your explorer somewhere, and in this script reference that. That way it will notice the changes

2 Likes

I’ll try that, thanks so much (dont mind this)

Sure ^_^. Mark as solution if it helps

1 Like

Hey I know that this has been solved but just wanted to add that if the value is going to be only LeftOn and LeftOff that repeat loop in a while loop is useless because the while loop will stop running as soon as the value is not LeftOn anymore so you can just move the code inside the repeat loop in the while loop