Loop breaks code

basically, i have a local script that works fine without the loop, but breaks with the loop. however, i need the loop so i can animate a static ui

code:

local Static_Index = 5
local Static_Elements = {
	Frame.Frame_1,
	Frame.Frame_2,
	Frame.Frame_3,
	Frame.Frame_4,
	Frame.Frame_5
}

local function StartStatic()
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut, 0, false, 0 )
	local tween1 = TweenService:Create(Frame.Frame_1, tweenInfo, {ImageTransparency = 0})
	local tween2 = TweenService:Create(Frame.Frame_2, tweenInfo, {ImageTransparency = 0})
	local tween3 = TweenService:Create(Frame.Frame_3, tweenInfo, {ImageTransparency = 0})
	local tween4 = TweenService:Create(Frame.Frame_4, tweenInfo, {ImageTransparency = 0})
	local tween5 = TweenService:Create(Frame.Frame_5, tweenInfo, {ImageTransparency = 0})
	tween1:Play()
	tween2:Play()
	tween3:Play()
	tween4:Play()
	tween5:Play()
	

	while task.wait(.05) do --THIS LOOP RIGHT HERE
		for index, Static in ipairs(Static_Elements) do
			Static_Elements[Static_Index].Visible = false
			Static.Visible = true
			Static_Index = index
		end
	end--]]

end

local function StopStatic()
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut, 0, false, 0 )
	local tween1 = TweenService:Create(Frame.Frame_1, tweenInfo, {ImageTransparency = 1})
	local tween2 = TweenService:Create(Frame.Frame_2, tweenInfo, {ImageTransparency = 1})
	local tween3 = TweenService:Create(Frame.Frame_3, tweenInfo, {ImageTransparency = 1})
	local tween4 = TweenService:Create(Frame.Frame_4, tweenInfo, {ImageTransparency = 1})
	local tween5 = TweenService:Create(Frame.Frame_5, tweenInfo, {ImageTransparency = 1})
	tween1:Play()
	tween2:Play()
	tween3:Play()
	tween4:Play()
	tween5:Play()
end

HitByBat.OnClientEvent:Connect(function()


	StartStatic()
	task.wait(3)

	StopStatic()

end)

(i deleted unneccisary parts)

1 Like

Loops will pause the thread that they’re in (until it has finished) unless you wrap it in a task.spawn or coroutine. Basically, because you have your while loop in StartStatic running every 0.05 seconds, it becomes task.wait(inf). You can adjust for this like so:

task.spawn(function()
    while task.wait(.05) do --THIS LOOP RIGHT HERE
	    for index, Static in ipairs(Static_Elements) do
		    Static_Elements[Static_Index].Visible = false
		    Static.Visible = true
		    Static_Index = index
	    end
    end--]]
end)
2 Likes

i tried this with coroutine, it didnt work. i dont have much experience with spawn or coroutine so is there other code i have to put instead of jus putting the loop in a spawn?

1 Like

I don’t believe so, it should work just fine.

Yeah, coroutines are tricky. I usually prefer using task.spawn instead.

2 Likes

yeah i mean the other code works, but the loop itself isnt doing anything/nothing is being animated

I’m not entirely sure how your UIs are set up, but you could try this and see if it works. I just shortened your script, and the reason why it isn’t working is because you’re setting the visibility back to back, in the same 2 lines of code. This change would be too fast to see.

for index, Static in ipairs(Static_Elements) do
	Static.Visible = not Static.Visible
end
1 Like

they are setup like this:
image
(that didnt work, it flickered it)
the loop i have cycles through the frames disabling and enabling them within .05 seconds each time

1 Like

Not sure what effect you’re trying to do here, could you send a screenshot of the static effects on-screen?

1 Like

I can’t seem to see anything from the video, sorry. I’m afraid I have to go now, sorry again. Good luck!

1 Like

oh wait never mind you task.spawn fixed it, perfect timing!

1 Like