Break a loop instantly

ProximityPrompt.Triggered:Connect(function(player)
	local playerCharacter = player.Character
	
	isBeingHacked.Value = true
	playerCharacter:MoveTo(script.Parent.PlayerPosition.Position)
	ReplicatedStorage.playerAnimation:FireClient(player, "Play", "hackAnimation")
	
	playerCharacter:FindFirstChild("Humanoid"):GetPropertyChangedSignal("MoveDirection"):Connect(function()
		isBeingHacked.Value = false
	end)

	while progress.Value < 100 do
		if not isBeingHacked.Value then
			break
		end
		
		task.wait(3)
		progress.Value = progress.Value + 5
		progressText.Text = progress.Value .. "%"
	end
	
	if progress.Value == 100 then
		workspace.HackedComputers.Value = workspace.HackedComputers.Value + 1

		isBeingHacked.Value = false
		isHacked.Value = true
	else
		print("player stopped hacking")
	end
	
	ReplicatedStorage.playerAnimation:FireClient(player, "Stop")
end)

the problem is when the player moves, it still waits that 3 seconds before it stops

1 Like

Ok this is how i would go about this

Maybe try checking the value at this portion of code again?

Could you use return in while loops

Im not at a pc rn so i cant test anything

It may be because you have a wait in your while loop, which delays the rest of the script from running. A possible solution could be running that while loop in a different thread or changing the GetPropertyChangeSignal function to not only set isBeingHacked to false but also stop playing the animation as well.

i cant stop the animation at the GetPropertyChangeSignal function cause it also affects the adding of the progress


	while progress.Value < 100 do
		if not isBeingHacked.Value then
			break
		else
		
		task.wait(3)
		progress.Value = progress.Value + 5
		progressText.Text = progress.Value .. "%"
                end
	end

Here is something you could try using coroutines:

-- this would be somewhere after "progress" is declared
local active = false

local updateValues = coroutine.wrap(function()
	active = true
	while progress.Value < 100 and active do
		progress.Value += 5 -- same thing but shorter to write
		progressText.Text = progress.Value .. "%"
		task.wait(3) -- moved to the end so that it doesn't continue to update after the loop is stopped
	end
end)
-- replace the loop with this in your ".Triggered" event

updateValues() -- call new function
while isBeingHacked.Value do task.wait() end -- wait until the "isBeingHacked.Value" is not true
active = false -- stop the loop
local updateValues = coroutine.wrap(function()
	active = true
	while progress.Value < 100 and active do
		progress.Value += 5 -- same thing but shorter to write
		progressText.Text = progress.Value .. "%"
		task.wait(3) -- moved to the end so that it doesn't continue to update after the loop is stopped
		if progress.Value == 100 then
			workspace.HackedComputers.Value = workspace.HackedComputers.Value + 1

			isBeingHacked.Value = false
			isHacked.Value = true
		end
	end
end)

ProximityPrompt.Triggered:Connect(function(player)
	local playerCharacter = player.Character
	
	isBeingHacked.Value = true
	playerCharacter:MoveTo(script.Parent.PlayerPosition.Position)
	ReplicatedStorage.playerAnimation:FireClient(player, "Play", "hackAnimation")
	
	playerCharacter:FindFirstChild("Humanoid"):GetPropertyChangedSignal("MoveDirection"):Connect(function()
		isBeingHacked.Value = false
	end)

	updateValues()
	while isBeingHacked.Value do task.wait() end
	active = false

	ReplicatedStorage.playerAnimation:FireClient(player, "Stop")
end)

I changed some stuff since when it didnt work the first time, im getting this error

image

Oh sorry, try doing this:

local updateValues = function() -- removed the coroutine.wrap()
	-- stuff in the function
end
local routine = coroutine.wrap(updateValues) -- wrapped it here
routine() -- call the new function
while isBeingHacked.Value do task.wait() end -- wait until the "isBeingHacked.Value" is not true
active = false -- stop the loop

the lua keyword “break” would break it instantly, if it isn’t breaking the loop instantly it is due to code within your loop is yielding the current thread, so you could do the condition to break the loop everytime you execute yieldable code, or just use coroutines or separate threads using, task.spawn

I just created two scripts. And I just disable the second script to stop the progress.

Script 1:

ProximityPrompt.Triggered:Connect(function(player)
	local playerCharacter = player.Character
	
	ProximityPrompt.Enabled = false
	isBeingHacked.Value = true
	
	--Sets the players position and makes them look at the monitor.
	playerCharacter:MoveTo(script.Parent.PlayerPosition.Position)
	playerCharacter.HumanoidRootPart.CFrame = CFrame.lookAt(playerCharacter.HumanoidRootPart.Position, Vector3.new(screenPart.Position.X, screenPart.Position.Y, screenPart.Position.Z))
	
	task.wait(1)
	
	script.Parent.ProgressScript.Enabled = true
	
	playerAnimationEvent:FireClient(player, "Play", "hackAnimationInstance") -- Plays the typing animation
	
	playerCharacter:FindFirstChild("Humanoid"):GetPropertyChangedSignal("MoveDirection"):Connect(function() -- Checks if the player moves, which stops the hacking.
		isBeingHacked.Value = false
		ProximityPrompt.Enabled = true
		script.Parent.ProgressScript.Enabled = false
		playerAnimationEvent:FireClient(player, "Stop") -- Stops the typing animation
	end)
	
	progress:GetPropertyChangedSignal("Value"):Connect(function()
		if progress.Value == 100 then -- Checks if its finished.
			workspace.HackedComputers.Value = workspace.HackedComputers.Value + 1

			isBeingHacked.Value = false
			isHacked.Value = true
			playerAnimationEvent:FireClient(player, "Stop") -- Stops the typing animation
		end
	end)
end)

Script 2:

while true do
	task.wait(1)
	progress.Value = progress.Value + 1
	progressText.Text = progress.Value .. "%"
end

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