Originally Posted by damptraktor
I'm trying to write a consumable counter/tracker addon and I'm having trouble figuring out how to store different players and their consumables.
Assuming I want to track a limited amount of potion, how would I set this up?
My idea was something like:
Using a function to check if a player is already present in the table, if not create a new table and tinsert it, the playername would be the key and another table would contain the potion info.
Servers are down atm and I'm quite new to lua so I just wanted to ask if this is the right approach.
|
Sure.
If you had some function to add an item to a player it could go something like this:
Code:
local myTable = {}
function AddPlayerItem(playerName, itemName, itemCount)
if (not myTable[playerName]) then
-- New player, so create an empty table for them under their name
myTable[playerName] = {}
end
-- Assign is as the item count. If you don't care about the count, you can just use 'true' instead
myTable[playerName][itemName] = itemCount
end
function PlayerHasItem(playerName, itemName)
if (myTable[playerName]) then
return myTable[playerName][itemName]
end
end
For the return value, LUA doesn't care if the index doesn't exist. It'll just return nil if the item is not found. You do have to check that the player exists though, otherwise you might be trying to index a nil value, which is a no-no.