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

Value will not change upon request. Why not?

So I am trying to make a short animation that loops. To make it as compact as I could, I devised a system. The value it creates, though, will not change when required to. It will stay at 1 frame and keep at that frame. Thank you for any help.

while true do
	for i = 1,22 do
		Num = 1
		CurrentFrame = script.Parent.Parent.Frames:FindFirstChild("Frame" .. Num)
		script.Parent.Image = CurrentFrame.Texture
		print("Frame == " .. Num )
		wait(0.08)
		if Num == 22 then
			Num = 1
		else
			Num = Num +1
		end
	end
end

The first frame will appear, and in the output it keeps on saying that it is on frame 1. The script does set the image, so I know that part is correct.

2 answers

Well, you've defined the 'Num' variable WITHIN the loop, so every time it loops back, it goes back to becoming one. Put the 'Num' variable directly after the while loop. So...

while true do
    Num = 1
    for i = 1,22 do
        CurrentFrame = script.Parent.Parent.Frames:FindFirstChild("Frame" .. Num)
        script.Parent.Image = CurrentFrame.Texture
        print("Frame == " .. Num )
        wait(0.08)
        if Num == 22 then
            Num = 1
        else
            Num = Num +1
        end
    end
end

2 comments

0

Now I feel so stupid! XD I always do the simplest of mistakes.

0

Well, we're all human! If I helped, thank me by accepting the answer!

Try doing this instead:

local x = 0

while true do
	x = (x + 1) % 22 -- This increases the value, but NEVER goes over 22.

	CurrentFrame = script.Parent.Parent.Frames:FindFirstChild("Frame" .. x + 1) -- Add one, because when you do 22 % 22, you get 0. 21 % 22 = 21.
	script.Parent.Image = CurrentFrame.Texture
	print("Frame == " .. x + 1)
	wait(0.08)
end

There is no need for a for loop. If you use the for loop, you don't need the "Num" variable, you could just use the 'i' variable.

while true do
	for i = 1, 22 do
		CurrentFrame = script.Parent.Parent.Frames:FindFirstChild("Frame" .. i)
		script.Parent.Image = CurrentFrame.Texture
		print("Frame == " .. i)
		wait(0.08)
	end
end

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