Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- keeps track of the clients inventory
- -- some references
- local invRemotes = game:GetService("ReplicatedStorage"):WaitForChild("Shared"):WaitForChild("Remotes"):WaitForChild("Inventory")
- local invRemoteEvent = invRemotes:WaitForChild("Event")
- local invRemoteFunction = invRemotes:WaitForChild("Function")
- --- the table
- local it = {}
- it.Inventory = {} -- the main items inventory
- it.InventorySize = 0
- -- client listening functions --
- it.connections = {
- newSlot = {};
- updateSlot = {};
- emptySlot = {};
- sizeChange = {};
- }
- -- the utility event functions
- function it:fireConnection(eventname,...)
- local t = self.connections[eventname]
- if not t then
- error("unrecognized event id")
- return
- end
- for i,f in pairs(t) do
- if typeof(f) == "function" then
- local c = coroutine.create(function(...)
- f(...)
- end)
- coroutine.resume(c,...)
- end
- end
- end
- -- the binding functions
- function it:bindTo(eventName,id,f)
- local t = self.connections[eventName]
- if not t then
- error("unrecognized event id")
- return
- end
- if t[id] then
- error("can't overwrite binding with same id (already function with that bind in index)")
- return
- else
- print(id .. " " .. tostring(f))
- t[id] = f
- end
- end
- function it:unbindFrom(eventName,id)
- local t = self.connections[eventName]
- if not t then
- error("unrecognized event id")
- return
- end
- t[id] = nil
- end
- -- update functions --
- function it:newSlot(i,id,amt)
- --print("slot @" .. i .. "; id : " .. id .. "; amt : " .. amt .. ";")
- self.Inventory[id] = {id = id; amt = amt}
- self:fireConnection("newSlot",i,id,amt)
- end
- function it:emptySlot(i)
- --print("empty slot @" .. i)
- self.Inventory[i] = nil
- self:fireConnection("emptySlot",i)
- end
- function it:updateSlot(i,id,amt)
- --print("slot @" .. i .. "; id : " .. id .. "; amt : " .. amt .. ";")
- local oldSlot = self.Inventory[id]
- local newSlot = {id = id; amt = amt}
- self.Inventory[id] = newSlot
- if not oldSlot or not (oldSlot.id == newSlot.id) then
- self:fireConnection("newSlot",i,id,amt)
- else
- self:fireConnection("updateSlot",i,id,amt)
- end
- end
- function it:incoming(t,...)
- if t == "NEWSLOT" then
- self:newSlot(...)
- elseif t == "EMPTYSLOT" then
- self:emptySlot(...)
- elseif t == "UPDATESLOT" then
- self:updateSlot(...)
- end
- end
- -- init function
- function it:Init() -- setups so it can start tracking immediately
- -- get current table
- local contents,size = invRemoteFunction:InvokeServer("REQINFO")
- if contents and size then
- self.Inventory = contents
- self.InventorySize = size
- end
- invRemoteEvent.OnClientEvent:Connect(function(...)
- self:incoming(...)
- end)
- end
- return it
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement