Lua - Reddit – Telegram
Lua - Reddit
31 subscribers
281 photos
31 videos
4.27K links
News and discussion for the Lua programming language.

Subreddit: https://www.reddit.com/r/lua

Powered by : @r_channels & @reddit2telegram
Download Telegram
Short Ai

Hi everybody, I made a small ai that kinda works. You can use it by using copy and pasting it and typing in the console commands like “I like dancing” or “I hate cats.”
math.randomseed(os.time()) -- Randomize for variability

-- Memory system to store user preferences
local memory = {}

-- Expanded knowledge base with new categories
local knowledgeBase = {
greetings = {
{ word = "hello", response = "Hey there! How’s it going?" },
{ word = "hi", response = "Hi! What’s up?" },
{ word = "hey", response = "Hey! How are you?" }
},
farewells = {
{ word = "bye", response = "Goodbye! See you soon." },
{ word = "goodbye", response = "Take care! Until next time." }
},
affirmatives = {
{ word = "yes", response = "Alright! That sounds good to me." },
{ word = "sure", response = "Sure thing!" }
},
negatives = {
{ word = "no", response = "That’s okay, maybe next time." },
{ word = "never", response = "Fair enough, we’ll leave it at that." }
},
commonwords = {
{ word = "I", response = "Tell me more about yourself!" },
{ word = "the", response = "Got it! Let's keep going." },
{ word = "and", response = "Alright, I’m listening!" },
{ word = "a", response = "That’s interesting! Keep going." }
},
emotions = {
{ word = "happy", response = "It’s great to hear you’re happy!" },
{ word = "sad", response = "I’m here for you if you want to talk." },
{ word = "excited", response = "Excitement is contagious! Tell me more!" }
},
actions = {
{ word = "run", response = "Running is a great way to stay fit!" },
{ word = "walk", response = "A nice walk can clear your mind." },
{ word = "jump", response = "Jumping around sounds fun!" }
},
animals = {
{ word = "dog", response = "Dogs are amazing companions!" },
{ word = "cat", response = "Cats can be so playful and curious!" },
{ word = "lion", response = "Lions are truly majestic creatures." }
},
nature = {
{ word = "mountain", response = "Mountains are breathtaking, aren’t they?" },
{ word = "river", response = "Rivers bring such peace and beauty." },
{ word = "forest", response = "Forests are full of life and mystery." }
},
colors = {
{ word = "red", response = "Red is such a bold and vibrant color!" },
{ word = "blue", response = "Blue always feels calm and peaceful." },
{ word = "green", response = "Green reminds me of nature and life!" }
}
}

-- Split input into words
function splitWords(input)
local words = {}
for word in input:lower():gmatch("%S+") do
table.insert(words, word)
end
return words
end

-- Find matches in the knowledge base
function findMatches(words)
for
, inputWord in ipairs(words) do
for , entries in pairs(knowledgeBase) do
for
, entry in ipairs(entries) do
if inputWord == entry.word:lower() then
return entry.response
end
end
end
end
return nil
end

-- Update memory system
function updateMemory(words)
local memoryKeywords = { "like", "love", "enjoy", "hate", "dislike" }
for i, word in ipairs(words) do
for , key in ipairs(memoryKeywords) do
if word == key and words[i + 1] then
local topic = words[i + 1]
memory[topic] = key
return "Got it! I’ll remember you " .. key .. " '" .. topic .. "'."
end
end
end
return nil
end

-- Generate response
function generateResponse(words)
-- Update memory if user specifies preferences
local memoryResponse = updateMemory(words)
if memoryResponse then return memoryResponse end

-- Find and return a response if a match exists
local response = findMatches(words)
if response then
return response
end

-- Check memory for known topics
for
, word in ipairs(words) do
if memoryword then
return "You told me you " .. memoryword .. " '" .. word .. "'. Do you still feel that way?"
end
end

-- Fallback response
return "Hmm, I’m not sure about that. Can you tell me more?"
end

-- Main Loop
print("AI Chat: Just type anything, and I’ll respond! Type 'exit' to quit.")
while true do
io.write("> ")
local input = io.read()
if input:lower() == "exit" then
print("Goodbye! Have a great day!")
break
else
local words = splitWords(input)
print(generateResponse(words))
end
end


https://redd.it/1hgot1a
@r_lua
Downgrade lua 5.3 para lua 5.1 e luarocks

# Atualmente eu estou usando lua 5.3 já instalei alguns apps nele como vlc, neovim, etc

# Quero saber se existe uma maneira de desinstalar o 5.3 e instalar o 5.1 sem quebrar os pacotes já instalado. ai vem a pergunta porque quero dar um downgrade estou tentando renderizar imagens no meu terminal kitty com plugin do neovim eu consigo já redenrizar com terminal puro mas sempre tenho problemas porque o plugin do neovim só funciona com lua 5.1 e eu já uso o 5.3 a um bom tempo. Alguem tem alguma dica do que fazer?

https://preview.redd.it/dnnp8bv20i7e1.png?width=843&format=png&auto=webp&s=e540b789d1f66cfd0f8050d80c269683efd2cbbd

https://preview.redd.it/as31cagy0i7e1.png?width=1210&format=png&auto=webp&s=8a968393a5fc9fbb23303adac0006863ad42b994




# Um exemplo do terminal renderizando puro e um exemplo de como fica meio bugado dentro do neovim mesmo não acusando erro no neovim nem no luarocks. Abaixo o exemplo da minha configuracao do plugin 3rd https://github.com/3rd/image.nvim

--
-- Filename: ~/github/dotfiles-latest/neovim/neobean/lua/plugins/image-nvim.lua
-- ~/github/dotfiles-latest/neovim/neobean/lua/plugins/image-nvim.lua

-- For dependencies see
-- ~/github/dotfiles-latest/neovim/neobean/README.md
--
-- -- Uncomment the following 2 lines if you use the local luarocks installation
-- -- Leave them commented to instead use luarocks.nvim
-- -- instead of luarocks.nvim
-- Notice that in the following 2 commands I'm using luaver
-- package.path = package.path
-- .. ";"
-- .. vim.fn.expand("$HOME")
-- .. "/.luaver/luarocks/3.11.05.1/share/lua/5.1/magick/?/init.lua"
-- package.path = package.path
-- .. ";"
-- .. vim.fn.expand("
$HOME")
-- .. "/.luaver/luarocks/3.11.0
5.1/share/lua/5.1/magick/?.lua"
--
-- -- Here I'm not using luaver, but instead installed lua and luarocks directly through
-- -- homebrew
-- package.path = package.path .. ";" .. vim.fn.expand("$HOME") .. "/.luarocks/share/lua/5.1/?/init.lua"
-- package.path = package.path .. ";" .. vim.fn.expand("$HOME") .. "/.luarocks/share/lua/5.1/?.lua"
-- Configuração para Lua 5.1 no Arch Linux
package.path = package.path
.. ";"
.. vim.fn.expand("$HOME")
.. "/.luarocks/share/lua/5.1/magick/?/init.lua"
.. ";"
.. vim.fn.expand("$HOME")
.. "/.luarocks/share/lua/5.1/magick/?.lua"
.. ";"
.. "/usr/share/lua/5.1/?.lua"
.. ";"
.. "/usr/lib/lua/5.1/?.lua"

package.cpath = package.cpath
.. ";"
.. vim.fn.expand("$HOME")
.. "/.luarocks/lib/lua/5.1/?.so"
.. ";"
.. "/usr/lib/lua/5.1/?.so"

return {
{
-- luarocks.nvim is a Neovim plugin designed to streamline the installation
-- of luarocks packages directly within Neovim. It simplifies the process
-- of managing Lua dependencies, ensuring a hassle-free experience for
-- Neovim users.
-- https://github.com/vhyrro/luarocks.nvim
"vhyrro/luarocks.nvim",
-- this plugin needs to run before anything else
priority = 1001,
opts = {
rocks = { "magick" },
},
},
{
"3rd/image.nvim",
enabled = true,
dependencies = { "luarocks.nvim" },
config = function()
require("image").setup({
backend = "kitty",
kittymethod = "normal",
integrations = {
-- Notice these are the settings for markdown files
markdown = {
enabled = true,
clear
ininsertmode = false,
-- Set this to false if you don't want to render images coming from
-- a URL
downloadremoteimages = true,
-- Change this if you would only like to render the image where the
-- cursor is at
-- I set this to true, because if the file has way too many images
-- it will be laggy and will take time for the initial load
onlyrenderimageatcursor = true,
-- markdown extensions (ie. quarto) can go here
filetypes = { "markdown", "vimwiki", "html" },
},
neorg = {
enabled = true,
clearininsertmode = false,
download
remoteimages = true,
only
renderimageatcursor = false,
filetypes = { "norg" },
},
-- This is disabled by default
-- Detect and render images referenced in HTML files
-- Make sure you have an html treesitter parser installed
-- ~/github/dotfiles-latest/neovim/neobean/lua/plugins/treesitter.lua
html = {
enabled = true,
only
renderimageatcursor = true,
-- Enabling "markdown" below allows you to view html images in .md files
--
https://github.com/3rd/image.nvim/issues/234
-- filetypes = { "html", "xhtml", "htm", "markdown" },
filetypes = { "html", "xhtml", "htm" },
},
-- This is disabled by default
-- Detect and render images referenced in CSS files
-- Make sure you have a css treesitter parser installed
-- ~/github/dotfiles-latest/neovim/neobean/lua/plugins/treesitter.lua
css = {
enabled = true,
},
},
max
width = nil,
maxheight = nil,
max
widthwindowpercentage = nil,

-- This is what I changed to make my images look smaller, like a
-- thumbnail, the default value is 50
-- maxheightwindowpercentage = 20,
max
heightwindowpercentage = 40,

-- toggles images when windows are overlapped
windowoverlapclearenabled = false,
window
overlapclearftignore = { "cmpmenu", "cmpdocs", "" },

-- auto show/hide images when the editor gains/looses focus
editor
onlyrenderwhenfocused = true,

-- auto show/hide images in the correct tmux window
-- In the tmux.conf add `set -g visual-activity off`
tmux
showonlyinactivewindow = true,

-- render image files as images when opened
hijackfilepatterns = { ".png", ".jpg", ".jpeg", ".gif", ".webp", ".avif" },
})
end,
},
}

https://redd.it/1hgon09
@r_lua
What’s the difference between “else” and “elseif”?

I am a beginner who just recently started learning off YouTube.

Most of the things I can make out what they mean after watching some videos

But I still don't understand the meaning of the "elseif" statement

I know some degree of visual programming (scratch...), so I for sure know what the "if" and "else" statement means.

But for "elseif", I don't quite understand what the statement does

Like I can say things like

>variable = 2

>if variable == 1 then

> print("blah")

>else

> print("blee")

(correct me if I made a mistake in my example)

Something like this

I figured if I use "elseif", the results will be the same

So what's the purpose of the "elseif" statement?

https://redd.it/1hgurz3
@r_lua
Where to start?

I want to start Lua for fun so I can program on Roblox, I really want to start learning but don’t know where to start. Most coding websites just throw you straight in, but I want the ABSOLUTE beginner help. What I want is like a website or tutorial on Youtube but I doesn’t matter about platform.

Please!!

https://redd.it/1hgzm21
@r_lua
Can one determine total gc memory allocated?

If I understand correctly, collectgarbage 'count' gives the amount of allocated memory at the time of invocation. Is there a way in standard Lua/LuaJIT to determine the total memory including that previously collected at the time of invocation? That is, is there a way without modifying Lua itself to determine/benchmark how allocation heavy a piece of code is over a particular run? I'm thinking of something like get-bytes-consed from the SBCL Lisp implementation. Something similar to *gc-run-time* might be nice too.

https://redd.it/1hh09uz
@r_lua
My lua noscripts do not work when I launch r6 through steam but when I load up a different account through Ubisoft they work can somone help me get them working for stream?

My lua noscripts do not work when I launch r6 through steam but when I load up a different account through Ubisoft they work can somone help me get them working for stream?

https://redd.it/1hh2yvd
@r_lua
How do i make a configuration file that a lua reads from?

Hi, i want to make my lua noscript read from a .cfg or a .txt or whatever format is the easiest to toggle things inside it's menu

The code for what controls the checkboxes, sliders, dropboxes looks like this:

local Controls = {
checkbox1 = false,
checkbox2 = false,
checkbox3 = false,
checkbox4= true,
Dropdown1 = 1,
Dropdown2 = 1,
Slider1 = 90,
Slider1Input = "",
Slider1Editing = false,
}

What i want is to have somewhere my lua reads from to see what to toggle on and off as a configuration file for the lua noscript, how can i do this, hopefully i gave enough information for people to help me, if not please let me know

https://redd.it/1hi0ur0
@r_lua
hello

https://preview.redd.it/wbn7acdk308e1.png?width=844&format=png&auto=webp&s=d4e48660728fd31ad8b66574865957f04e389313

Hello, I have a problem with the Fivem server because on the paid hosting the noscript package does not work, but on the free one it already works and I keep getting the following error

https://redd.it/1hij6m2
@r_lua
Just a Lil help

Could any Lua/Luau master give me some tips on how to improve my programming? I've already learned a lot from some tutorials on YouTube (a lot). My goal is just to learn enough to be able to create a game on Roblox, as a start, of course.


Thx

https://redd.it/1hivu43
@r_lua
LUA state in 2025?

Hello everyone,

2024 is coming to an end and I'm quite curious about this, what is the current state of LUA in 2025? On the website of LUA, I feel like there's not much of an update. When I search LUA 2024 on Google, the result seems to stop at 2023. There are not many discussions, jobs for LUA also seem to be no result too. So I wonder, what is LUA going to be like in 2025?


The question seems to be vague, I hope you can understand as English is not my first language.

https://redd.it/1hj48nl
@r_lua
I want to build a small forum using Lua.

Hello!
I expressed my purpose in the noscript.
What library would you recommend to use?
Something like Python-Flask.
If it has basic support for creating endpoints, requests and templating I am happy.
I heard Lapis is the most mature.

https://redd.it/1hj9f9f
@r_lua
What is the best way to give a class “private” members?

Giving a class its members in lua implicitly makes them public.

https://redd.it/1hjou4b
@r_lua
Javanoscript "this" function but in lua?

I'm here in roblox getting stuck at this.

Javanoscript has this cool thing here where it is like

function button() {
print(this)
}

And the "this" will show whatever button executed this function, it can be fifty button with that function and it is a cool thing, but what about lua?

Button.TouchSwipe:Connect(function(SwipeDirection, Number, Processed)
print("??")
end)

How the hell do i do it, and roblox lua is a little different than lua right?

https://redd.it/1hk0gvh
@r_lua
Help with inconsistent iterator behavior

I am familiar with the concept of iterators in other languages, but I can't figure out how to get normal iterator behavior out of lua tables. The top example works normally with string iterators, but the bottom does not with table iterators.

-- works
stringEntries = string.gmatch("text1,text2,text3", "(^,+)")
print(stringEntries)
-- function: 0x5627c89b62c0
print(stringEntries())
-- text1

-- does not work
tableEntries = pairs({
    name = {"John", "Jane", "Bob"},
    age = {30, 25, 40},
    city = {"New York", "Los Angeles", "Chicago"}
})
print(tableEntries)
-- function: 0x5627946f14f0
print(tableEntries())
-- stdin:7: bad argument #1 to 'tableEntries' (table expected, got no value)
-- stack traceback:
-- C: in function 'tableEntries'
-- stdin:7: in main chunk
-- C: in ?

I would expect it to return the next key value pair but it's saying the tableEntries iterator expected a table as an argument? What argument would I give to an iterator function created from a table already?

Is there some other normal table iterator function that yields a normal iterator instead of whatever pairs does/does not do?

Edit: Code got repeated twice, removed duplicate, added outputs

https://redd.it/1hk4nay
@r_lua
Lua garbage collection

In Programming in Lua chapter 11.6 - String Buffers the author demonstrates a pitfall for reading files that can be slow due to how garbage collection works in Lua. I'm trying to test this myself so I created a 350KB text file and tried to read it using this code:

local buff = ""
for line in io.lines('data.txt') do
buff = buff .. line .. "\n"
end

The author claimed that reading a file of this size using this code would take almost a minute. But in my case it executed in less than a second. Are the docs outdated? Is there some under the hood optimization? or am I doing something differently?

https://redd.it/1hk6ekt
@r_lua
REDM Coding Issue

I have a friend coding a REDM server and he needs the correct hash for the Lancaster Repeater. I have no idea where to look for this in the RDR2 list and he's pulling his hair trying to figure it out. Can anyone help?

https://redd.it/1hkbwf9
@r_lua
Is there a way to convert a .lua file into a .xml file?

I am trying to make a mod of a game that used .xml files, I want to know if there is a way to convert it so I don't have to learn a whole now programing language. If you need any information on what I am using, I will gladly provide

https://redd.it/1hkc7i5
@r_lua
How do i make a lua loading system

Hey, it might seem like a weird question but i’m using luajit, i saw someone make a lua loader inside of their lua in order to interact with the menu in the way of making checkboxes, sliders dropdowns etc

How would someone go about doing this? I have a menu ready to go with checkboxes, sliders, dropdowns etc but

Is this an api specific thing or is it a luajit thing?

https://redd.it/1hkghhv
@r_lua