Advertisement
Guest User

ClientSideInventorySystem

a guest
Nov 14th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 0 0
  1. -- keeps track of the clients inventory
  2.  
  3. -- some references
  4. local invRemotes = game:GetService("ReplicatedStorage"):WaitForChild("Shared"):WaitForChild("Remotes"):WaitForChild("Inventory")
  5. local invRemoteEvent = invRemotes:WaitForChild("Event")
  6. local invRemoteFunction = invRemotes:WaitForChild("Function")
  7.  
  8. --- the table
  9. local it = {}
  10. it.Inventory = {} -- the main items inventory
  11. it.InventorySize = 0
  12.  
  13. -- client listening functions --
  14. it.connections = {
  15.     newSlot = {};
  16.     updateSlot = {};
  17.     emptySlot = {};
  18.     sizeChange = {};
  19. }
  20. -- the utility event functions
  21. function it:fireConnection(eventname,...)
  22.     local t = self.connections[eventname]
  23.     if not t then
  24.         error("unrecognized event id")
  25.         return
  26.     end
  27.     for i,f in pairs(t) do
  28.         if typeof(f) == "function" then
  29.             local c = coroutine.create(function(...)
  30.                 f(...)
  31.             end)
  32.             coroutine.resume(c,...)
  33.         end
  34.     end
  35. end
  36. -- the binding functions
  37. function it:bindTo(eventName,id,f)
  38.     local t = self.connections[eventName]
  39.     if not t then
  40.         error("unrecognized event id")
  41.         return
  42.     end
  43.     if t[id] then
  44.         error("can't overwrite binding with same id (already function with that bind in index)")
  45.         return
  46.     else
  47.         print(id .. "   " .. tostring(f))
  48.         t[id] = f
  49.     end
  50. end
  51.  
  52. function it:unbindFrom(eventName,id)
  53.     local t = self.connections[eventName]
  54.     if not t then
  55.         error("unrecognized event id")
  56.         return
  57.     end
  58.     t[id] = nil
  59. end
  60.  
  61.  
  62. -- update functions --
  63. function it:newSlot(i,id,amt)
  64.     --print("slot @" .. i .. "; id : " .. id .. "; amt : " .. amt .. ";")
  65.     self.Inventory[id] = {id = id; amt = amt}
  66.     self:fireConnection("newSlot",i,id,amt)
  67. end
  68.  
  69. function it:emptySlot(i)
  70.     --print("empty slot @" .. i)
  71.     self.Inventory[i] = nil
  72.     self:fireConnection("emptySlot",i)
  73. end
  74.  
  75. function it:updateSlot(i,id,amt)
  76.     --print("slot @" .. i .. "; id : " .. id .. "; amt : " .. amt .. ";")
  77.     local oldSlot = self.Inventory[id]
  78.     local newSlot =  {id = id; amt = amt}
  79.     self.Inventory[id] = newSlot
  80.     if not oldSlot or not (oldSlot.id == newSlot.id) then
  81.         self:fireConnection("newSlot",i,id,amt)
  82.     else
  83.         self:fireConnection("updateSlot",i,id,amt)
  84.     end
  85. end
  86. function it:incoming(t,...)
  87.     if t == "NEWSLOT" then
  88.         self:newSlot(...)
  89.     elseif t == "EMPTYSLOT" then
  90.         self:emptySlot(...)
  91.     elseif t == "UPDATESLOT" then
  92.         self:updateSlot(...)
  93.     end
  94. end
  95. -- init function
  96. function it:Init() -- setups so it can start tracking immediately
  97.     -- get current table
  98.     local contents,size = invRemoteFunction:InvokeServer("REQINFO")
  99.     if contents and size then
  100.         self.Inventory = contents
  101.         self.InventorySize = size
  102.     end
  103.     invRemoteEvent.OnClientEvent:Connect(function(...)
  104.         self:incoming(...)
  105.     end)
  106. end
  107.  
  108. return it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement