Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Value will not change upon request. Why not?

Asked by 10 years ago

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

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

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

0
Now I feel so stupid! XD I always do the simplest of mistakes. MisaMiner 50 — 10y
0
Well, we're all human! If I helped, thank me by accepting the answer! Shawnyg 4330 — 10y
Ad
Log in to vote
0
Answered by
Tkdriverx 514 Moderation Voter
10 years ago

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

Answer this question