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

Making a variable's value go up to a certain point then down?

Asked by
816i 19
7 years ago
Edited 7 years ago

I made a variable for a part of my script but this doesnt seem to work, the print is so I know what the value is, why doesn't it work?

local wag = 44
while true do
    for i = 1, 45 do
        if wag <= -44 then
            wag = wag+1
            wait(.5)
        elseif wag >= 44 then
            wag = wag-1
            wait(.5)
        end
    end
    print(wag)
end

EDIT: Can someone fix this so it goes from 44 to -44 then back up?

1
What if wag is in-between -44 and 44 OldPalHappy 1477 — 7y
0
Oh yeah, can someone fix this so it goes from -44 to 44 816i 19 — 7y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

I'm gonna show a way that's different - using a bool switch. Bool switches are just variables that you turn "on" and "off" each iteration.

You check the bool switch and execute code accordingly.

local wag = 44; --number
local bool = false; --bool switch
local MIN,MAX = -44,44 --bounds

while true and wait() do
    if wag < MIN or wag > MAX then --If number is out of bounds
        bool = not bool --switch the bool
    end
    wag = (bool and wag + 1) or wag - 1 --increment value according to bool
    print(wag)
end
Ad

Answer this question