Lua expected = near data - lua

local file = 'data.txt'
local lines = lines_from(file)
global data = {} -- this line is giving the error "lua: day8.lua:20: '=' expected near 'data'"
for key, value in pairs(lines) do
local row = {}
for i=1, #value do
local char = value:sub(i,i)
row[i] = char
end
data[key] = row
end
As you can see from the code above, my code is throwing an error on the line where the variable data is initialised. This code was working earlier when I tested it, then I added more code below what is visible and it broke this line somehow.
I don't think its the code below that broke this line as otherwise why would it be showing there?
This is also my first ever code with lua so I have no experience with this language.
What could be wrong in this code that could cause this error

Global variables don't need to be declared explicitly like local ones do. The interpreter is erroring because you are prefixing global to your variable, which isn't a recognized keyword
Try the following, without the global:
data = {}

Related

roblox LUA Expected 'end' (to close 'than' at line 3), got '='

function teleportTo(placeCFrame)
local plyr = game.Players.LocalPlayer;
if plyr.Character then
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame;
end
end
teleportTo(game:GetService("Workspace").game:GetService("Workspace").Zeppelin.FuelTank1.Tank.CFrame)
my code is here, idk much about coding thanks for helping
and if you told me how to make the player teleport to a moving object it would be so super
The error is telling you what is going on.
When the code was being interpreted line by line, it expected the next symbol to be end, but instead it got =.
That means that something about how you're using the equals sign is incorrect. So when we look at the line :
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame
You cannot assign a value on the same line as a return command. return is used to pipe a value out of a function so it can be used where the function was called.
But I'm pretty sure that isn't what you intended, you just want to set a player's position. So to fix your issue, remove the return.
if plyr.Character then
plyr.Character.HumanoidRootPart.CFrame = placeCFrame
end
The reason you are getting an error is because = is usually defined as setting a value, and no code can be executed after a return or it will error
If you wanted to, you could add return after all the code is done executing

how to fix Players.louie43pro.PlayerGui.admin.Frame.LocalScript:10: Expected identifier when parsing expression, got ')' error roblox studio?

I have a problem in Roblox studio. There's an error when im trying to make an admin command script using local script.
The error is
Players.louie43pro.PlayerGui.admin.Frame.LocalScript:10: Expected
identifier when parsing expression, got ')'
I have tried so many things to fix this error.
please help.
This is the script that i have
local rs = game:GetService("ReplicatedStorage")
local commandEvent = rs:WaitForChild("CommandEvent")
local frame = script.Parent
local enter = frame.Enter
local box = frame.CommandBox
--takes command and splits it up
local function get_command()
local command = ()
for word in string.gmatch(box.Text, "\S+") do
table.insert(command, word)
end
local action = command[1]
local person = command[2]
print(action)
print(person)
commandEvent:FireServer(action, person)
box.Text = " "
end
enter.MouseButton1Click:Connect(get_command)
Can anybody help me with my problem?
please..
local command = () is not valid Lua syntax.
() is the call operator. You want to create a table value so use the table constructor {}
local command = {}
Or maybe you forgot a function name? You could also do something like this
local command = getMeSomeTable()
Then the call operator would make sense. That function should return a table of course.
As soon as you have fixed this error you'll face another error for using an invalid escape sequence. Replace \S with %S to match a non-whitespace character.
In case you do not find 2 matches you'll run into the next error. So you should check if command actually has two elements at keys 1 and 2 befor you index them.
So maybe add something like
if action and person then command:FireServer(action, person) end

lua - check if userdata is nil [duplicate]

I have a lua script with code block as below:
local call_data = cjson.decode(ARGV[1])
local other_data = cjson.decode(ARGV[2])
local data = {}
local next = next
local populate_data = function(source)
if next(source) == nil then
return
end
for property,value in pairs(source) do
redis.call('HSET', KEYS[2], property, value)
end
end
populate_data(call_data)
populate_data(other_data)
When I try to run the script with the following command KEYS and ARGV as:-
redis-cli --eval dialed.lua "inflight_stats:18" "calls:AC443d7a8111a96ba8074f54a71f0521ce:CA1ec49703eee1959471c71506f43bb42e:dialed" , "{\"from\":\"+18035224181\",\"to\":\"+919943413333\",\"sid\":\"CA1ec49703eee1959471c71506f43bb42e\",\"status\":\"queued\",\"direction\":\"outbound-api\",\"date_created\":null,\"account_sid\":\"AC443d8a8111a96ba8074f54a71f0521ce\"}" "{\"phone\":\"919943413333\",\"campaign_id\":18,\"caller_session_sid\":\"CA828b163153bf5cc301ef5285e38925f9\"}" 0
Error :-
(error) ERR Error running script (call to f_08dcc69ee8baa0200e0cf552948ab4bc338c9978): #user_script:11: #user_script: 11: Lua redis() command arguments must be strings or integers
TL;DR for values returned by cjson.decode(), use cjson.null to compare to JSON's null value.
Explanation: Lua uses nil in tables to mark deleted entries. If JSONinc nulls were converted to Lunatic nils, the decoded objects would be corrupt. Therefore, the cjson lib uses a lightweight userdata type to represent null/nil.
Your 'call_data' has a 'date_created' field that is null - that causes the error.
The funny thing is that Redis, like Lua, will not store a nil/null value, so you'll have to either ignore null values or use a special value in Redis to flag them.
Assuming you'll be ignoring them, here's one way around it:
local call_data = cjson.decode(ARGV[1])
local other_data = cjson.decode(ARGV[2])
local data = {}
local next = next
local null = cjson.null
local populate_data = function(source)
if next(source) == nil then
return
end
for property,value in pairs(source) do
if value ~= null then
redis.call('HSET', KEYS[2], property, value)
end
end
end
populate_data(call_data)
populate_data(other_data)
Also, a small optimization would be to batch the updates, like so:
local payload = {}
for property,value in pairs(source) do
if value ~= null then
table.insert(payload, property)
table.insert(payload, value)
end
end
redis.call('HSET', KEYS[2], unpack(payload))
P.S. if you want, look at ReJSON that I wrote - it is designed to help with what it appears that you're trying to do.

Filling Lua table not going as expected

I am coding a little something in Lua and I've encountered a very frustrating bug/mistake in my code.
network = {}
network.neurons = {}
for i=1,4 do
network.neurons[20000] = {}
network.neurons[20000][i] = NewNeuron()
print(network.neurons[20000][i])
end
The NewNeuron() function creates a new object with some variables. The print() inside this for loop returns the table with the correct variables as expected. The problem comes when I try to use this print again in this loop:
for i=1,4 do
print(network.neurons[20000][i])
end
The print then writes 4 console lines as follows:
(no return)
(no return)
(no return)
*neuron info that should be printed*
It looks as if only the last of the 4 objects exists after I exit the creation loop. Why is this? What am I doing wrong?
You are assigning an entirely new table inside the loop when creating a NewNeuron. The declaration should be outside:
network = {}
network.neurons = {}
network.neurons[20000] = {}
for i=1,4 do
network.neurons[20000][i] = NewNeuron()
print(network.neurons[20000][i])
end

Wireshark Dissector in Lua

First of all, I'm new to Lua altogether, and this is my first attempt at writing a wireshark dissector.
My protocol is straightforward - a 2 byte length field, followed by a string of that length.
When I run the code through the Lua console, everything works as expected.
When the code is added to the Wireshark plugins directory, I get the error
Lua Error: [string "C:\Users...\AppData\Roaming\Wireshark..."]:15: calling 'add' on bad self (number expected, got string)
Line 15 corresponds is the t:add(f_text... line.
Can anyone explain the discrepancy between the execution methods?
do
local p_multi = Proto("aggregator","Aggregator");
local f_len = ProtoField.int16("aggregator.length","Length",base.DEC)
local f_text = ProtoField.string("aggregator.text","Text")
p_multi.fields = { f_len, f_text }
local data_dis = Dissector.get("data")
function p_multi.dissector(buf,pkt,root)
pkt.cols.protocol = "Aggregator"
local len = buf(0,2):int()
local t = root:add(p_multi,buf(0,len+2))
t:add(f_len,buf(0,2),"Length: " .. buf(0,2):int())
t:add(f_text,buf(2,len),"Text: " .. buf(2,len):string())
end
local tcp_encap_table = DissectorTable.get("tcp.port")
tcp_encap_table:add(4321,p_multi)
end
Your dissector code is very close to correct, but you're doing extra work that the interface won't accept. If you change your dissector function like so,
function p_multi.dissector(buf,pkt,root)
pkt.cols.protocol = "Aggregator"
local len = buf(0,2):int()
local t = root:add(p_multi,buf(0,len+2))
t:add(f_len,buf(0,2)) --let Wireshark do the hard work
t:add(f_text,buf(2,len)) --you've already defined their labels etc.
end
you'll get the desired behavior. The labels "Text" and "Length" are already defined for your fields, so there is no need to provide them again on lines 15 and 16.

Resources