Lua Minecraft ComputerCraft function issue - lua

Okay so here is my function:
function button1()
mon.clear()
sleep(.25)
shell.run("movie")
end
Says: "attempt to index ? (a nil value)" for the shell.run("movie") line

I found out the problem.
You can't call shell.run() from another program using os.loadAPI()

so you have declared the mon variable and the function() there are two choices
switch the this command to shell.run("monitor side movie")
rewrite the program and in the function replace mon.setCursorPos(1.1) to term.setCursorPos(1,1) then when you run the program type "monitor side program" ok

Related

Why do I use 'end)' instead of just end |?

draw.RoundedBox(0,0,0,100,100,Color(120,255,120))
end)
I'm watching tutorials for learning Lua, more specifically Lua for the Garry's Mod engine. During the tutorial, I noticed using just 'end' didn't work. I had to use 'end)' with a ')'.
Why do I have to place a ')' at the end of end in this code?
Some functions take other functions as parameters.
When you see a function called like this:
hello(param, function(a) print(a) end)
(Sorry, poor example. This function doesn't really do anything) The function on the inside is called internally from the source of the hello function.
The end is the end of the inside function, and the ) is the end of the parameter list for the first function.
So here we have a function hook.Add() being called:
hook.Add("HUDPaint", "DrawMyHud", function()
draw.RoundedBox(0,0,0,100,100,Color(120,255,120))
end)
and from the GMOD wiki:
hook.Add( string eventName, any identifier, function func )
--Add a hook to be called upon the given event occurring.
Although we're calling a function, what the function is doing is creating a special connection in the code called a hook. On the event "HUDPaint", the GMOD client will call the function provided.
Another way to write this function that might make the meaning of end) more clear is by making each parameter it's own line:
hook.Add(
"HUDPaint",
"DrawMyHud",
function() draw.RoundedBox(0,0,0,100,100,Color(120,255,120)) end --end of function declaration
) --end of hook.Add function call

PANIC: unprotected error in call to Lua API (init.lua:116: attempt to call field 'alarm' (a nil value))

I keep getting an error on this line of code, how can I solve this? Thanks in advance
tmr.alarm(0, 250, tmr.ALARM_AUTO, function()
You are obviously using an outdated example snippet from somewhere. There is no tmr.alarm function in the timer module.
See https://nodemcu.readthedocs.io/en/latest/modules/tmr/ for the current API documentation. There is a alarm() function on a timer object i.e. you first need to create a timer object. The below example is straight from the documentation:
if not tmr.create():alarm(5000, tmr.ALARM_SINGLE, function()
print("hey there")
end)
then
print("whoopsie")
end

How to determine if sysdig field exists or handle error if it doesn't

I'm using Sysdig to capture some events and have a small chisel (LUA script) to capture and format the events as necessary. On the on_init() I'm requesting fields like so :
f_field = chisel.request_field("<field>")
My question is how can I check if a field exists before requesting it? I'm going to use a new field only just released on 0.24.1 but ideally I'd like my chisel to continue to work on older versions of sysdig without this field. I've tried wrapping the call to chisel.request_field in a pcall() like so :
ok, f_field = pcall(chisel.request_field("<field>"))
and even implementing my own "get_field" function :
function get_field(field)
ok, f = pcall(chisel.request_field(field))
if ok then return f else return nil end
end
f_field = get_field("<field>")
if f_field ~= nil then
-- do something
end
but the error ("chisel requesting nonexistent field <field>") persists.
I can't see a way to check if a field exists but I can't seem to handle the error either. I really don't want multiple versions of my scripts if possible.
Thanks
Steve H
You're almost there. Your issue is in how you're using pcall. Pcall takes a function value and any arguments you wish to call that function with. In your example you're passing the result of the request_field function call to pcall. Try this instead..
ok, f = pcall(chisel.request_field, "field")
pcall will call the chisel method with your args in a protected mode and catch any subsequent errors.

Difference between "end" and "end)"?

A while ago I had stumbled upon script tutorials where end was used. But then, a few pages later, I've found an end) instead of just end. So I was troubled by this, what is the closing bracket meant to do?
There is no end) syntax. There is end and ), both with their own meanings. end closes a block (initiated with if, for, do, while or function) and ) closes something that started with (, an expression in parentheses, parameter list etc. You have to use end where a block was started, and use ) where something was started with (.
Since the only way a block can appear in an expression is via a function, both those parts of syntax can appear together, if a function is a part of a complex expression or an argument list. However, it's nothing special really, something like end} or end] can appear the same way.
local func = function() print("hello") end
string.dump(func)
This is the same as:
string.dump(function() print("hello") end)
end is a part of the function syntax (function expression), and ) is a part of the ( syntax (function call here).
The first line in the first piece of code can be also written like this:
local func = (function() print("hello") end)
Here, ) just closes the first parenthesis, and the expression is identical to the original one.
I assume you're talking about ROBLOX's modded version of Lua, if so then
end) would end :connect, for example.
workspace.part.Touched:connect(function(part)
--stuff
end)
If not then refer to llidanS4's answer.

Garrys Mod lua kick and gag Troubble

I've got a Server and want to add tabs to the menu I've put in the command
local force = actions:AddOption( "Kick Player from the game" )
force:SetIcon( "icon16/delete.png" )
function force:DoClick()
RunConsoleCommand("ulx kick","reason", ply:EntIndex())
end
But i Get This Error sign
RunConsoleCommand: Command has invalid characters! (ulx kick (' '))
The first parameter of this function should contain only the command, the second parameter should contain arguments.
PLEASE Can anyone help me
Your command is ulx
Your arguments are: kick, reason and ply:EntIndex()
So instead of
RunConsoleCommand("ulx kick","reason", ply:EntIndex())
the line should be:
RunConsoleCommand("ulx", "kick", ply:EntIndex(), "reason")
'cause the first argument of the function should only contain the command you want to call.
Source: your Error Message ("The first parameter of this function should contain only the command, [...]") and
http://wiki.garrysmod.com/page/Global/RunConsoleCommand

Resources