I'm trying to put together an autofishing script for Terraria that will do more than just click at scheduled intervals. At this point, it's giving me a syntax error at the line that says while fishing do.
I've tried separating the while and the do to different lines, putting the fishing into parentheses, putting something else between the line before and the while loop in case it's the line before actually causing the problem. The only thing that any of any of those accomplished was when I put the do on the next line. When I did that it complained about the line with just do.
I'm pretty new to Lua scripting, but it looks like the exact same sort of while loop as I've seen in the documentation.
fishing = false
function goFish()
PressAndReleaseKey("d")
Sleep(5)
PressAndReleaseKey("d")
PressAndReleaseKey("1")
local x = GetRunningTime()
while fishing do
if(GetRunningTime() % 180000) == 0) then PressAndReleaseKey("b") end
PressAndReleaseMouseButton(1)
Sleep(4500)
if(GetRunningTime()-x > 6000000) then
x = getBait()
end
end
end
The error is at this line:
if(GetRunningTime() % 180000) == 0) then PressAndReleaseKey("b") end
which should be
if(GetRunningTime() % 180000) == 0 then PressAndReleaseKey("b") end
or
if((GetRunningTime() % 180000) == 0) then PressAndReleaseKey("b") end
In Lua IF conditions do not need to be wrapped in parentheses.
Related
I'm trying to make a function that searches through some code to find the line the search term is on, as well as the line's index. The code is a multi line string with new line characters. I was thinking of using gmatch to do this, but I have no clue how.
This is my code at the moment. It's awful, but I can't think of a way to make it any better:
local function search( code, term )
local matches = {}
local i = 0
for line in string.gmatch( code, "[^\r\n]+" ) do
i = i + 1
if string.find( line, term, 1, true ) then
table.insert( matches, { line = i, code = line } )
end
end
return matches
end
Any help would be appreciated!
Your solution seem fine to me. The problem with using a single gmactch loop is your requirement to report line numbers. The code below avoids this by embedding line numbers into the code. I've use # to mark line numbers. You can use any char that does not appear in the source code, even something like \0.
function search(code,term)
for a,b in code:gmatch("#(%d+):([^\n]-"..term.."[^\n]-)\n") do
print(a)
print(b)
end
end
local n=0
code="\n"..code
code=code:gsub("\n", function () n=n+1 return "\n#"..n..":" end)
search(code,"matc")
I'm learning Lua and trying to create a simple coroutine. In Lua 5.1, the code below gives the error: "attempt to yield across metamethod/C-call boundary." I've read about that limitation and I can't see how it applies to my code. I tried it in Lua 5.2 and got "attempt to yield from outside a coroutine," which is equally confusing to me. I'm sure the answer will be embarrassingly obvious!
output = {}
done = false
function mainLoop()
while not done do
if co == nil then
co = coroutine.create(subLoop())
elseif coroutine.status(co) == "suspended" then
print(output[k])
coroutine.resume(co)
elseif coroutine.status(co) == "dead" then
done = true
end
end
end
function subLoop()
for k=1, 20 do
table.insert(output, "This is line " .. k .. " of the test output")
coroutine.yield()
end
end
mainLoop()
You are calling subLoop
if co == nil then
co = coroutine.create(subLoop())
instead of passing it to coroutine.create
if co == nil then
co = coroutine.create(subLoop)
This results in you attempting to yield from the main state / (not-really-)coroutine, which gives errors with varying descriptions across versions.
I'm trying to find out a way to use a multiline comment on a batch of code, but it keeps mistaking some syntax in it as a ]] and thinking I want it to end there, which I don't!
--[[
for k,v in pairs(t) do
local d = fullToShort[k]
local col = xColours[v[1]] -- It stops here!
cecho(string.format(("<%s>%s ", col, d))
end
--]]
I thought I read somewhere it was possible to do use a different sort of combination to avoid those errors, like --[=[ or whatnot... Could someone help?
As you can see in Strings tutorial there is a special [===[ syntax for nesting square braces. You can use it in block comments too. Just note, that number of = signs must be the same in open and close sequence.
For example 5 equals will work.
--[=====[
for k,v in pairs(t) do
local d = fullToShort[k]
local col = xColours[v[1]] -- It stops here!
cecho(string.format(("<%s>%s ", col, d))
end
--]=====]
You can use the following to create multiline comments past ]]'s:
--[[
codes
]]
Im a amatuer at coding. So, mind me if i face palmed some things.
Anyways, im making a alpha phase for a OS im making right? I'm making my installer. Two questions. Can i get a code off of pastebin then have my lua script download it? Two. I put the "print" part of the code in cmd. I get "Illegal characters". I dont know what went wrong. Here's my code.
--Variables
Yes = True
No = False
--Loading Screen
print ("1")
sleep(0.5)
print("2")
sleep(0.5)
print("Dowloading OS")
sleep(2)
print("Done!")
sleep(0.2)
print("Would you like to open the OS?")
end
I see a few issues with your code.
First of all, True and False are both meaningless names - which, unless you have assigned something to them earlier, are both equal to nil. Therefore, your Yes and No variables are both set to nil as well. This isn't because true and false don't exist in lua - they're just in lowercase: true and false. Creating Yes and No variables is redundant and hard to read - just use true and false directly.
Second of all, if you're using standard lua downloaded from their website, sleep is not a valid function (although it is in the Roblox version of Lua, or so I've heard). Like uppercase True and False, sleep is nil by default, so calling it won't work. Depending on what you're running this on, you'll want to use either os.execute("sleep " .. number_of_seconds) if you're on a mac, or os.execute("timeout /t " .. number_of_seconds) if you're on a PC. You might want to wrap these up into a function
function my_sleep_mac(number_of_seconds)
os.execute("sleep " .. number_of_seconds)
end
function my_sleep_PC(number_of_seconds)
os.execute("timeout /t " .. number_of_seconds)
end
As for the specific error you're experiencing, I think it's due to your end statement as the end of your program. end in lua doesn't do exactly what you think it does - it doesn't specify the end of the program. Lua can figure out where the program ends just by looking to see if there's any text left in the file. What it can't figure out without you saying it is where various sub-blocks of code end, IE the branches of if statements, functions, etc. For example, suppose you write the code
print("checking x...")
if x == 2 then
print("x is 2")
print("Isn't it awesome that x is 2?")
print("x was checked")
lua has no way of knowing whether or not that last statement, printing the x was checked, is supposed to only happen if x is 2 or always. Consequently, you need to explicitly say when various sections of code end, for which you use end. For a file, though, it's unnecessary and actually causes an error. Here's the if statement with an end introduced
print("checking x...")
if x == 2 then
print("x is 2")
print("isn't it awesome that x is 2?")
end
print("x was checked")
although lua doesn't care, it's a very good idea to indent these sections of code so that you can tell at a glance where it starts and ends:
print("checking x...")
if x == 2 then
print("x is 2")
print("isn't it awesome that x is 2?")
end
print("x was checked")
with regards to your "pastebin" problem, you're going to have to be more specific.
You can implement sleep in OS-independent (but CPU-intensive) way:
local function sleep(seconds)
local t0 = os.clock()
repeat
until os.clock() - t0 >= seconds
end
I use very simple Lua scripting in an online game called ROBLOX. My problem is that values in my scripts aren't changing! Example:
num = 0
while true do
num = num + 1
print(num)
wait(1)
end
That should count up starting on 0, but the number won't change. Could this be from the ROBLOX website? I can't figure out what else it might be.
What happens with
local num = 0
while true do
num = num + 1
print(num)
wait(1)
end
?
Maybe some other part of the system is changing the global num.
I just put your code in the Lua demo and it works fine if you remove the wait() function call. I'm assuming you defined this function somewhere?
There is nothing wrong with the code. You must be running it wrong. Also, wait is a function defined in the Roblox API. It is legit.
There is no error in your code. If you are using ROBLOX, then I'm not sure how you're running it wrong as it is a fairly simple interface. I'll try it in ROBLOX and see if it errors for me.
To the people who were wondering about wait(): it's a ROBLOX-specific global function, that pauses the current task the amount of seconds in the parentheses.
Try this:
local num = 0
while true do
num = num + 1
print(num)
print(type(num))
wait(1)
end