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
Help Script lua

I posted it again because for some reason my post became unavailable. I wanted someone to change this noscript to activate on mouse button (5) and deactivate on mouse button (4), because I tried and couldn't do it. I would appreciate it if anyone could make the change.



function OnEvent(event, arg)

OutputLogMessage("event = %s, arg = %d\\n", event, arg)

if (event == "PROFILE_ACTIVATED") then

EnablePrimaryMouseButtonEvents(true)

elseif event == "PROFILE_DEACTIVATED" then

ReleaseMouseButton(2) -- to prevent it from being stuck on

end

if (event == "MOUSE_BUTTON_PRESSED" and arg == 4) then

recoil = not recoil

spot = not spot

end

if (event == "MOUSE_BUTTON_PRESSED" and arg == 1 and recoil) then

if recoil then

spot = not spot

repeat

Sleep(3)

MoveMouseRelative(0,7)

Sleep(9)

MoveMouseRelative(0,3)

Sleep(12)

MoveMouseRelative(0,14)

Sleep(25)

until not IsMouseButtonPressed(1)

end

end

end

https://redd.it/1iqz975
@r_lua
Confusion on local variables

Learning lua for fun but confused by local varibles. So I get that local varibles are limited to the code block they are in. But if you make a local varible like this

local name = “jack”

print(name)

In the code the varible can be accessed from anywhere in the program. Does this not defeat the purpose of a local varible as now you can access it from anywhere ? Why not just make this varible global ?

https://redd.it/1irvcd7
@r_lua
Learning

I want to learn Lua. What's the best way to start? Should I just watch YouTube tutorials, or should I try some websites first?

https://redd.it/1it6osp
@r_lua
How Lua Scripting in Redis Ensures TBMQ as a High-Performance MQTT Broker

Achieving 8,900 messages per second per CPU core and scaling to 1 million messages per second—with even more capacity on the horizon. By migrating from Postgres to Redis for persistent MQTT sessions and leveraging Lua noscripting, we eliminated a major performance bottleneck.

In our latest blog post, we share the challenges we faced and the architectural decisions that led to these impressive results. We also explain how Lua noscripting in Redis can optimize persistent caching layers, dramatically offloading database workloads. This enables real-time processing with minimal latency and guaranteed delivery, improving both scalability and performance.

Whether you’re a software engineer looking for technical ideas and patterns or a manager aiming to future-proof the infrastructure of your system, you’ll find valuable insights to enhance your system efficiency and make it reliable and scalable. 

Read the full story on our blog to learn how we achieved these breakthroughs.

Ready to try it out? Check out our GitHub.

https://redd.it/1it9kj3
@r_lua
Need help with scaling object speed to player score

Hi there - very new to programming. Only started learning Lua a few days ago.

I am at the stage where I've been trying to tweak the code of a 'Pong' tutorial I followed to make it my own. The basic idea is that you control a cheeseburger trying to escape a fat guy. The fat guy is programmed to always pursue the player. Player score goes up in seconds, with it resetting everyone's positions if the fat guy makes contact with the player.

I have finally got everything to work, except I am having real trouble scaling the fat guys speed to make the game harder the longer you survive.

Here is the code for my fat guy:

Carson = {}



function Carson:load()

self.img = love.graphics.newImage("assets/CARSON.jpg")

self.width = self.img:getWidth()

self.height = self.img:getHeight()

self.x = love.graphics.getWidth() / 1.5

self.y = love.graphics.getHeight() / 2 - (self.height / 2)

self.speed = 90

self.xVel = -self.speed

self.yVel = 10

end



function Carson:update(dt)

Carson:chasePlayer(dt)

speedBoost(dt)

end



function speedBoost(dt)

self.speed = self.speed + Score.score

end



function Carson:chasePlayer(dt)

if self.x + (self.width / 2) > Player.x + (Player.width / 2) then

self.speed = -self.speed -- Possibly irrelevant code

self.x = self.x + self.xVel * dt

elseif Player.x + (Player.width / 2) > self.x + (self.width / 2) then

self.x = self.x + -self.xVel * dt

end



if self.y + (self.height / 2) > Player.y + (Player.height / 2) then

self.y = self.y + self.xVel * dt

elseif Player.y + (Player.width / 2) > self.y + (self.height / 2) then

self.y = self.y + -self.xVel * dt

end

end



function Carson:draw()

love.graphics.draw(self.img, self.x, self.y)

end

I cannot work out for the life of me why this won't work. In theory, I want the function speedBoost() to update the self.speed to whatever it started at, plus the players score (since the players score goes up in seconds, this should result in an enemy whos speed directly scales with how long you've survived). I am pulling Score.score from my main.lua file in love.load():


function love.load()

Player:load()

Carson:load()



Score = {score = 0, highscore = 0}

font = love.graphics.newFont(30)

end

However when I try and boot the game I'm getting: carson.lua:20: attempt to index global 'self' (a nil value).


I figured that since I have Carson:update(dt) in love.update(dt), love.update was attempting to call the self.speed, resulting in the error, so I changed the speedBoost function to:

function speedBoost(dt)

Carson.speed = Carson.speed + Score.score

end

Which allows me to run the game without any error, however the fat guy just doesn't ever get any faster.


Updating self.speed manually to a higher number seems to work fine, but any attempt to automate this by scaling it with the player score or even delta time either gives me an error or the fat guy just doesn't speed up.


What am I missing? I'm driving myself mad.

https://redd.it/1italma
@r_lua
Lua origins and security

At a recent cybersecurity conference, an answer from one of a panelist suggested Lua was a security risk. The question was about device automation and TAA certification of hardware. The panelist referred to QSC, saying that it was off-limits for them (a DoD contractor) because the native language is Lua, and Lua has its origins in Brazil, "a BRICS country". Baffled, I later looked it up and indeed the QSC platform, Q-Sys, uses Lua.

Has anybody ever heard of Lua being classed as a security risk because it originates from Brazil??

https://redd.it/1itdwp1
@r_lua
Lapis Framework

hey! i want to start using the Lapis framework,

I'm a complete beginner in Lua and i have some questions

What version of Lua to use with Lapis? i heard the 5.4 is not the best for Lapis, so which one should I use?

my main OS is windows, should i use a WSL? (to install lua, luarocks & lapis)



https://redd.it/1ithzvp
@r_lua
Anybody figured how to use Luamidi with Love2D on MacOS?

It's giving some errors and it says it can only be compatible with Lua 5.1. I installed that manually but man, it isn't working and giving syntax errors, which mostly indicates compatibility issues with the versions of Lua / LuaMidi and Love2D I'm using (and i think love2D uses LuaJIT) Has anybody figured this out? cuz i'm trying out really stupid but fun projects involving both, and I need my midi input from piano (real-time) to work to get those apps running

https://redd.it/1itsd8r
@r_lua
why does this lua pattern has no match

for word in string.gmatch('camelCase', '^%l+') do
print(word) // camel expected here but nothing
end


https://redd.it/1iuow0m
@r_lua
What would you add to my Lua code?

I am starting in programming and I made this small game for the user to guess the right number, and I would like to know what you, as an intermediate or advanced programmer, would add to my code.

print([

('-. .-') .-') .-') _ _ .-') .-. .-') ('-. _ .-')
_( OO) ( OO ). ( OO ). ( OO ) ) ( '.( OO )_ \ ( OO ) _( OO)( \( -O )
,----. ,--. ,--. (,------.(_)---\_)(_)---\_) ,--./ ,--,' ,--. ,--. ,--. ,--.);-----.\(,------.,------.
' .-./-') | | | | | .---'/ _ | / _ | | \ | |\ | | | | | `.' | | .-. | | .---'| /`. '
| |_( O- )| | | .-') | | \ :` `. \ :` `. | \| | )| | | .-') | | | '-' /_)| | | / | |
| | .--, \| |_|( OO )(| '--. '..`''.) '..`''.) | . |/ | |_|( OO )| |'.'| | | .-. `.(| '--. | |_.' |
(| | '. (_/| | | `-' / | .--' .-._) \.-._) \ | |\ | | | | `-' /| | | | | | \ || .--' | . '.'
| '--' |(' '-'(_.-' | `---.\ /\ / | | \ | (' '-'(_.-' | | | | | '--' /| `---.| |\ \
`------' `-----' `------' `-----' `-----' `--' `--' `-----' `--' `--' `------' `------'`--' '--'
])
local number = math.random(1, 100)

io.write("Type a number: ")
local guess = tonumber(io.read())

if guess == number then
print("\nCongratulations")
elseif guess ~= number then
while guess ~= number do
if number > guess then
print("\nTry a higher number")
io.write("Type a number: ")
guess = tonumber(io.read())
if guess == number then
print("\nCongratulations you guessed the number!\n")
end
elseif number < guess then
print("\nTry a smaller number")
io.write("Type a number: ")
guess = tonumber(io.read())
if guess == number then
print("\nCongratulations you guessed the number!\n")
end
end
end
end


https://redd.it/1iv3p2y
@r_lua
Trying to make an 'or' entry, please help me out?

Hi guys,

For a personal mod in Assetto Corsa, I'm trying to let a LUA file - that switches on the app when it sees to right car - look for not one, but two cars: the 2025 car and the 2024 car.
Nothing I do seems to work:
" , "
" or "
making the 5 (or 4) into %s

These are the lines; can someone please point me in the right direction?



if string.startsWith(ac.getCarID(0), "formula_2_2025") then appActive = true

local function optimizeCarModel(car)
if ac.getCarID(car.index) ~= "formula22025” then
return
end

https://redd.it/1ivh7sg
@r_lua
Dluau: luau runtime focused on extensibility and modularity

Project link

A personal project i've been working on. Luau is a great variant of lua in my opinion, but its support for dynamically extending the environment has always been limited. This project tries to bridge that gap a little on top of some other extra quality-of-life features.

The project is subdivided into a minimal core, which exports all the Luau C API symbols as well as some extra API for registering userdata tags, namecall atoms and types in a dynamic library friendly way. And a standard library (which is kinda all over the place at the moment) which you can optionally use.

Statically resolves noscript, DLL and standard library dependencies at startup of the runtime only loading the dependencies you are actually using. Then sandboxes these resources.

Any tips, feedback, suggestions or critique would be greatly appreciated.

https://redd.it/1ivtmij
@r_lua
Lua Game Engine export multi platform application only bytecode

Hello

I'am looking for a recommendation of a (2D) Lua game engine which can be used to develop games.

I want a multi platform export of the application but with only bytecode in the bundle/package (I don't want to ship the Lua source code in the bundle/package).

Any recommendation?

Thank you

https://redd.it/1ivtw09
@r_lua
I've made a very small programming language in Lua in 2 days

So I've created IBScript because I was bored at a math lesson and this idea came to me.

The name was selected because It is badly written :/

here is the GitHub page: https://github.com/illersaver/IBScript

https://redd.it/1ivx93n
@r_lua
Require

I still haven’t been able to find how to do this in lua:

How do i require luas i don’t know the full name of?

For example Require all luas with Addon or Plugin at the end

I’ve seen a lua that requires all luas that have Addon at the end from a certain directory like SomethingAddon.lua and it works requires all luas from a directory

I would look at the code of it but it’s obfuscated with a custom obfuscator so i thought my only option was asking here how i do that

https://redd.it/1iwn5qw
@r_lua
Is this possible to deobfuscate?

\\149\\163\\221\\137\\1469\\148!\\198\\212,qz\\136


If not tell me an moonsec V3 Deobfuscator that works :) ty

https://redd.it/1ix57m5
@r_lua
Variations in the hash function used in Lua tables

Running Lua 5.4.7 here. I've found that the output of the following program can change from one run to another.

t = { "a"=1, "b"=1 }

for k, v in pairs(t) do
io.write(k)
end
io.write("\n")

Using the standard Lua interpreter, sometimes it outputs ab and sometimes ba.

Now, I understand that the order in which pairs iterates through the keys in a table is unspecified -- as with most hash tables. But I find it interesting that the order can actually vary between runs on the same machine with the same version of Lua.

Some details: if I start up a Lua interactive environment and run the above code with dofile, then the order is consistent until I quit the interactive environment. But if I start up a new interactive environment, then the order may be different.

So, I take it that, when the standard Lua interpreter is initialized, there is some pseudorandomness or time dependence in the choice of the hash function used for tables.

I thought I had a question, but I guess I really don't. Although if someone has more info on this issue -- e.g., exactly what is done, and why, and whether this happens in all versions of Lua -- I'd love to hear about it.

https://redd.it/1ix9hhl
@r_lua
better Lua fail value?

In the Lua docs it mentions fail which is currently just nil.

I don't personally like Lua's standard error handling of returning nil, errormsg -- the main reason being it leads to awkward code, i.e. local val1, val2 = thing(); if not val1 then return nil, val2 end

I'm thinking of designing a fail metatable, basically just a table with __tostring that does string.format(table.unpack(self)) and __call that does setmetatable so you can make it with fail{"bad %i", i}. The module would also export a isfail(v) function that just compares the getmetatable to the fail table as well as assert that handles a fail object (or nil,msg).

So the code would now be local val1, val2 = thing(); if isfail(val1) then return val1 end

Has anyone else worked in this space? What are your thoughts?



https://redd.it/1ixndr0
@r_lua
local varients optimization

I heard that using "local" keywords are very important in Lua. But I knew that "local" keyword in algorithm coding are not good at optimization. No difference, sometimes take long time to be executed.

Why "local" keyword is nesessary? Is it really efficient? I can't figure out why these things are happening.

(But if you don't use local keyword in function, This can make error)

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