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

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

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?

2 comments

1

What if wag is in-between -44 and 44

0

Oh yeah, can someone fix this so it goes from -44 to 44

1 answer

✓ Accepted answer

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

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