local playerStats = {health = 100, stamina = 50, mana = 30}
local count = 0
for key, value in pairs(playerStats) do
count = count + 1
end
print(count) -- Output: 3
local inventory = {}
-- เพิ่มไอเท็มในคลัง
function addItem(item)
table.insert(inventory, item)
print(item .. " added to inventory.")
end
-- ลบไอเท็มออกจากคลัง
function removeItem(index)
if inventory[index] then
print(inventory[index] .. " removed from inventory.")
table.remove(inventory, index)
else
print("Item not found at index " .. index)
end
end
-- แสดงจำนวนไอเท็มในคลัง
function countItems()
print("You have " .. #inventory .. " items in your inventory.")
end
-- ตัวอย่างการใช้งาน
addItem("Sword")
addItem("Shield")
addItem("Potion")
countItems() -- Output: You have 3 items in your inventory.
removeItem(2) -- Output: Shield removed from inventory.
countItems() -- Output: You have 2 items in your inventory.
ความคิดเห็น
แสดงความคิดเห็น