Well firstly that won't work, It'll error along the lines of "cannot use … outside of vararg function" or something to that tune.
However allowing for your typo this would be my first suggestion
local function f(...) --# localise the function
local arg = tonumber((...)) or 1 --# localise the variables
local x = 0
while x < arg do
if turtle.forward() then --# no need for == true, the function returns a boolean no need to do true == true, just use the returned boolean
x = x + 1
else
turtle.dig()
end
end
end
But this can be further improved by not using a while loop, but instead using a for loop. A for loop will cycle from the first specified number through to the second, at the increment specified by the optional third (when absent +1 is assumed) example loops:
--# basic increment
for i = 1, 10 do
print("Ten times ", i)
end
--# changed increment
for i = 1, 10, 2 do
print("Five times ", i)
end
--# decrementing
for i = 10, 1, -1 do
print("Ten times ", i)
end
--# would not run as 15 is > than 10
for i = 15, 10 do
print("You don't see me")
end
so therefore we can change you loop to
local function f(...)
local arg = tonumber((...)) or 1
for i = 1, arg do
--# this change is needed so that it will try to move forward until it does
while not turtle.forward() do --# the not keyword inverts the result so not true = false and not false = true
turtle.dig()
end
end
end
This code is starting to come along quite nicely, however there is one more change that can be made, and that is to specify an argument name as opposed to using a vararg, since we only want one argument not an unspecified amount, so we can then do
local function f(times)
times = tonumber(times) or 1
for i = 1, times do
while not turtle.forward() do
turtle.dig()
end
end
end
Now as distantcam stated while I was typing this up, you could put the evaluation inside the conditional, but don't let anyone tell you to do this, ever, it is inefficient as you're having to make that evaluation/conversion each time it loops, as opposed to just once.
Hope all this helped and makes sense
— BIT
EDIT: Oh also incase you're interested, the reason you should use local variables and functions is
It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones.
Reference
Also as for the name of your function, I do suggest that you get in a habit of better naming conventions like calling it
multiForward, or even
forward, instead of just
f it can make the world of difference later on down the track when you're trying to debug something, or if someone else is reading your code.