Learn LUA with Real Code Examples
Updated Nov 21, 2025
Code Sample Descriptions
1
Lua Button Press Counter
count = 0
function updateUI()
print('Button Count: ' .. count)
end
function buttonPress()
count = count + 1
updateUI()
end
-- Simulate actions
updateUI()
buttonPress()
buttonPress()
Counts button presses and prints the count to the console.
2
Lua Theme Toggle
isDark = false
function updateUI()
print('Theme: ' .. (isDark and 'Dark' or 'Light'))
end
function toggleTheme()
isDark = not isDark
updateUI()
end
-- Simulate actions
updateUI()
toggleTheme()
toggleTheme()
Toggles between Dark and Light themes.
3
Lua Score Tracker
score = 0
function updateUI()
print('Score: ' .. score)
end
function increment()
score = score + 10
updateUI()
end
function decrement()
score = score - 5
updateUI()
end
function reset()
score = 0
updateUI()
end
-- Simulate actions
updateUI()
increment()
decrement()
reset()
Tracks a score with increment, decrement, and reset.
4
Lua Simple Timer
time = 0
function updateUI()
print('Time: ' .. time .. ' sec')
end
function tick()
time = time + 1
updateUI()
end
-- Simulate actions
updateUI()
tick()
tick()
Counts seconds and prints elapsed time.
5
Lua Health Tracker
health = 100
function updateUI()
print('Health: ' .. health)
end
function damage(amount)
health = health - amount
updateUI()
end
function heal(amount)
health = health + amount
updateUI()
end
-- Simulate actions
updateUI()
damage(20)
heal(10)
Tracks health with damage and healing.
6
Lua Level Tracker
level = 1
function updateUI()
print('Level: ' .. level)
end
function nextLevel()
level = level + 1
updateUI()
end
function resetLevel()
level = 1
updateUI()
end
-- Simulate actions
updateUI()
nextLevel()
nextLevel()
resetLevel()
Tracks game levels with increment and reset.
7
Lua Coin Counter
coins = 0
function updateUI()
print('Coins: ' .. coins)
end
function collectCoin()
coins = coins + 1
updateUI()
end
function loseCoin()
coins = coins - 1
updateUI()
end
-- Simulate actions
updateUI()
collectCoin()
collectCoin()
loseCoin()
Counts coins collected and lost in a game.
8
Lua Ammo Tracker
ammo = 10
function updateUI()
print('Ammo: ' .. ammo)
end
function shoot()
ammo = ammo - 1
updateUI()
end
function reload()
ammo = 10
updateUI()
end
-- Simulate actions
updateUI()
shoot()
shoot()
reload()
Tracks ammo usage with shoot and reload actions.
9
Lua Star Collector
stars = 0
function updateUI()
print('Stars: ' .. stars)
end
function collectStar()
stars = stars + 1
updateUI()
end
function loseStar()
stars = stars - 1
updateUI()
end
-- Simulate actions
updateUI()
collectStar()
collectStar()
loseStar()
Counts collected stars and displays progress.
10
Lua Power-Up Timer
powerTime = 10
function updateUI()
print('Power-Up Time: ' .. powerTime)
end
function tick()
powerTime = powerTime - 1
updateUI()
end
function resetPower()
powerTime = 10
updateUI()
end
-- Simulate actions
updateUI()
tick()
tick()
resetPower()
Counts down power-up duration and resets.