Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to break while true do loop inside of a if then statement?

So how would I be able to break this loop immediately when the value is no longer 2?

Would I make another "if then" statement to break the loop?

Like how would I do this (for example)

If Value is (not equal to) ~= 3 then "break"

This is the script I want to do this to.


--Jog Right Animation
SpriteAnimationValue.Changed:Connect(function()
playerframe.ClipsDescendants = true	
greenframe.ClipsDescendants = false	
	local RightJogAnimationSpeed = 0.05
if AnimationValue.Value == 2 then
	wait()
	playerframe.Size = UDim2.new(0,115,0,118)
	playerframe.AnchorPoint = Vector2.new(0,0)
	playerframe.Position = UDim2.new(0,0,0,0)	
	
	while AnimationValue.Value == 2 do
sonicsprite.Position = UDim2.new(0,-606+17,0,-4)
	wait(RightJogAnimationSpeed)
sonicsprite.Position = UDim2.new(0,-769+17,0,-7)
	wait(RightJogAnimationSpeed)
sonicsprite.Position = UDim2.new(0,-918+17,0,-7)
	wait(RightJogAnimationSpeed)
sonicsprite.Position = UDim2.new(0,-1072+17,0,-7)
	wait(RightJogAnimationSpeed)
sonicsprite.Position = UDim2.new(0,-1189+17,0,-7)
	wait(RightJogAnimationSpeed)
sonicsprite.Position = UDim2.new(0,-15+17,0,-149)
end
end
end)

1 comment

0

'break' is actually a lua keyword that you can use to break loops.

1 answer

This should stop the animation as soon as AnimationValue.Value changes to something other than 2

--Jog Right Animation

local function updateSonicSpritePosition(udim2)
    if Animation.Value ~= 2 then return end -- Check here on every func call
    sonicsprite.Position = udim2
    wait(RightJogAnimationSpeed)
end

SpriteAnimationValue.Changed:Connect(function()
    playerframe.ClipsDescendants = true 
    greenframe.ClipsDescendants = false 
    local RightJogAnimationSpeed = 0.05
    if AnimationValue.Value == 2 then
        wait()
        playerframe.Size = UDim2.new(0,115,0,118)
        playerframe.AnchorPoint = Vector2.new(0,0)
        playerframe.Position = UDim2.new(0,0,0,0)
        while AnimationValue.Value == 2 do
            updateSonicSpritePosition(UDim2.new(0,-606+17,0,-4))
            updateSonicSpritePosition(UDim2.new(0,-769+17,0,-7))
            updateSonicSpritePosition(UDim2.new(0,-918+17,0,-7))
            updateSonicSpritePosition(UDim2.new(0,-1072+17,0,-7))
            updateSonicSpritePosition(UDim2.new(0,-1189+17,0,-7))
            updateSonicSpritePosition(UDim2.new(0,-15+17,0,-149))
        end
    end
end)

1 comment

0

Thanks man, though this doesn't exactly solve my problem. Some animations play through others when the AnimationValue changes. I just need a way to break the while true do loops.

This discussion is preserved as it was. New answers, comments, and votes are closed.