Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How Would I Make an Inventory System with Tables?

Asked by 4 years ago

When making RPG games, most if not all of them include an inventory system that works like this:

Let's say there are 3 items. each labeled Item1,Item2 and Item3.

Now, if I were to remove Item2, then Item3 would slide into the position of Item2 and Item2 would be equipped.

By observing other scripters and developers, I found that I would have to use a table to conduct such things. I have no experience in making tables, all I do know is that a table contains values that can be summoned with TableName.value. I also do know that you have to summon a table with curly brackets ({,}).

Sorry if it's hard to read, but I'm really desperate. Can anyone show me examples of a table and how I would use it to create an inventory system?

Regards, Your pal Sensei_Developer.

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Hey Sensei_Developer

This is a really interesting question you've asked.

What I would do is, have a server script;

local data = {} -- create a table, and name it 'data'

game.Players.PlayerAdded:Connect(function() -- player added event.
    data[plr.Name] = {"Starting Weapon", "Another Weapon"} -- create a new table inside of the 'data' table, name it after the player's name, and give them a weapon.
    print(data[plr.Name][1]) -- Would print > 'Starting Weapon'
    print(data[plr.Name][2]) -- Would print > 'Another Weapon'
end)

So with this code, there are multiple things you can do, I'll list a few!

Use Datastore service, to load and save. (Check for data else give them starting weapon) Fire the client, to update their inventory. Using remove events.

To add a weapon:

table.insert(data[plr.Name], #data[plr.Name]+1, "Hello") -- will insert hello, to position two in the players table.
print(data[plr.Name][3]) -- Would print > 'Hello'

To remove a weapon:

function getIndex(tbl, weaponName) -- new function named, getIndex.
    for i,v in pairs(tbl) do -- for loop, to iterate though the table
        if v == weaponName then -- if the weaponName = the current iteration value
            return i -- return the index of 'weaponName'
        end
    end
end

table.remove(data[plr.Name], getIndex(data[plr.Name], "Starting Weapon")) -- Would remove the starting Weapon.

I hope this has helped you a little.

I am fairly new to tables myself, but I hope this helped

Please feel free to comment back, alternatively contact me on discord, and let me know who you are. Invoke_Client#0100

0
Hello, InvokeClient. I do understand some bits, but how would I use the table you created and implement it into the GUI? Sensei_Developer 298 — 4y
0
My discord tag is Senseisushi#1865 Sensei_Developer 298 — 4y
Ad

Answer this question