FeedBack On Inventory System

So i wrote a script for a inventory. Some things arent complete yet like combining stacks but the core is done. I want some feedback on the script or some way that i can make it better

local script

local inputservice = game:GetService("UserInputService")
wait(5)
local Player = game.Players.LocalPlayer
inputservice.InputBegan:connect(function(i,g)
	if i.UserInputType == Enum.UserInputType.Keyboard then
		if i.KeyCode == Enum.KeyCode.E then
			for _,Door in pairs(workspace.Items:GetChildren()) do
				local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
				if Mag <= Door.Range.Value then
					Door.Event:FireServer()
					break
				end
			end
		end
	end
end)


while true do
	script.Parent.ImageLabel.Visible = false
	for _,Door in pairs(workspace.Items:GetChildren()) do
		local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
		if Mag <= Door.Range.Value then
			local D3ToD2 = workspace.CurrentCamera:WorldToScreenPoint(Door.Center.Position)
			script.Parent.ImageLabel.Visible = true
			script.Parent.ImageLabel.Position = UDim2.new(0,D3ToD2.X,0,D3ToD2.Y,0)
			script.Parent.ImageLabel.TextLabel2.Text = Door.Text.Value
			break
		end
	end
	wait()
end

server script

local inventorydata = game:GetService("DataStoreService"):GetDataStore("inventorydata")
local RS = game.ReplicatedStorage
local down = RS.Events.Inventory.DOWN
local up = RS.Events.Inventory.UP
local inv = {one = "Thompson:1:100/100:one:rbxassetid://674010691", two = "M4A1:1:100/100:two:rbxassetid://129934018", three = false, four = false, five = false, six = false, seven = false, eight = false, nine = false, ten = false, eleven = false, twelve = false, thirteen = false, fourteen = false, fifteen = false, sixteen = false, seventeen = false, eighteen = false, nineteen = false, twenty = false, twentyone = false, twentytwo = false, twentythree = false, twentyfour = false, twentyfive = false, twentysix = false, twentyseven = false, twentyeight = false, twentynine = false, thirty = false, thirtyone = false, thirtytwo = false, thirtythree = false, thirtyfour = false, thirtyfive = false, thirtysix = false, thirtyseven = false, thirtyeight = false, thirtynine = false, fourty = false, fourtyone = false, fourtytwo = false, fourtythree = false, fourtyfour = false, fourtyfive = false, fourtysix = false, fourtyseven = false, fourtyeight = false, fourtynine = false, fiftyone = false, fiftytwo = false, fiftythree = false, fiftyfour = false, fiftyfive = false, fiftysix = false, fiftyseven = false}
print(inv)
local HttpsService = game:GetService("HttpService")
local ocuupid = {one = true, two = true, three = false, four = false, five = false, six = false, seven = false, eight = false, nine = false, ten = false, eleven = false, twelve = false, thirteen = false, fourteen = false, fifteen = false, sixteen = false, seventeen = false, eighteen = false, nineteen = false, twenty = false, twentyone = false, twentytwo = false, twentythree = false, twentyfour = false, twentyfive = false, twentysix = false, twentyseven = false, twentyeight = false, twentynine = false, thirty = false, thirtyone = false, thirtytwo = false, thirtythree = false, thirtyfour = false, thirtyfive = false, thirtysix = false, thirtyseven = false, thirtyeight = false, thirtynine = false, fourty = false, fourtyone = false, fourtytwo = false, fourtythree = false, fourtyfour = false, fourtyfive = false, fourtysix = false, fourtyseven = false, fourtyeight = false, fourtynine = false, fiftyone = false, fiftytwo = false, fiftythree = false, fiftyfour = false, fiftyfive = false, fiftysix = false, fiftyseven = false}
--[[inventory template = 
{one = name:number:durability:slot:image}
]]
local held
local current
local checkinfo
local ammount = 0
local plr
game.Players.PlayerAdded:Connect(function(pl)
	plr = pl
	local info
	print("goin")
	pcall(function()
		info = inventorydata:GetAsync(pl.UserId)
		print(pl.UserId)
	end)
	if info then
		local deocde = HttpsService:JSONDecode(info)
		inv = deocde
	end
	pcall(function()
		info = inventorydata:GetAsync(pl.UserId .. "OCC")
	end)
	if info then
		local deocde = HttpsService:JSONDecode(info)
		ocuupid = deocde
	end
	update(pl)
	updatetool()
end)
game.Players.PlayerRemoving:Connect(function(pl)
	local encode = HttpsService:JSONEncode(inv)
	inventorydata:SetAsync(pl.UserId, encode)
	encode = HttpsService:JSONEncode(ocuupid)
	inventorydata:SetAsync(pl.UserId .. "OCC", encode)
end)
game:BindToClose(function()
	local players = game.Players:GetPlayers()
	for _, player in pairs(players) do
		local encode = HttpsService:JSONEncode(inv)
		inventorydata:SetAsync(player.UserId, encode)
		encode = HttpsService:JSONEncode(ocuupid)
		inventorydata:SetAsync(player.UserId .. "OCC", encode)
	end
end)
function updatetool()
	for slot, occ in pairs(inv) do
		if occ ~= false then
			local firstvalue = occ
			firstvalue = string.split(firstvalue, ":")
			local name = firstvalue[1]
			local number = firstvalue[2]
			local durability = firstvalue[3]
			local imageone = firstvalue[5]
			local imagetwo = firstvalue[6]
			local slotwon = firstvalue[4]
			local image = imageone .. ":" .. imagetwo
			local tool = game.ReplicatedStorage.Items:FindFirstChild(name)
			local clone = tool:Clone()
			clone.INFO.Slot.Value = slot
			clone.INFO.Ammount.Value = number
			clone.Parent = plr.Backpack
		end
	end
end
function changeocc(name, value)
	for slot, occ in pairs(ocuupid) do
		if slot == name then
			ocuupid[slot] = value
		end
	end
end
function copy(name)
	for slot, occ in pairs(inv) do
		if slot == name then
			local value = occ
			inv[slot] = false
			return value
		end
	end
end
function paste(name, value)
	for slot, occ in pairs(inv) do
		if slot == name then
			inv[slot] = value
		end
	end
end
function update(player)
	repeat wait() until player.PlayerGui:WaitForChild("Inv")
	for slot, occ in pairs(inv) do
		if occ == false then
			if player ~= nil then
				RS.Events.Inventory.ANTI:FireClient(player, 0, "", slot)
			end
		else
			if player ~= nil then
				local target = player.PlayerGui.Inv.Main:FindFirstChild(slot)
				local firstvalue = occ
				firstvalue = string.split(firstvalue, ":")
				local name = firstvalue[1]
				local number = firstvalue[2]
				local durability = firstvalue[3]
				local imageone = firstvalue[5]
				local imagetwo = firstvalue[6]
				local slotwon = firstvalue[4]
				local image = imageone .. ":" .. imagetwo
				local slot = player.PlayerGui.Inv.Main:FindFirstChild(slot)
				slot.Image = image
				RS.Events.Inventory.ANTI:FireClient(player, number, image, slot)
			end
		end
	end
end
function holdocc(hold)
	for slot, occ in pairs(ocuupid) do
		if slot == hold.Name then
			if occ == true then
				return true
			else
				return false
			end
		end
	end
end
function switch(player)
	for slot, occ in pairs(ocuupid) do
		if held ~= nil and current ~= nil and current ~= held then
			if slot == current.Name then
				if occ == false then
					local firstimage = held.Image
					local secondimage = current.Image
					current.Image = firstimage
					held.Image = secondimage
					local firstvalue = copy(held.Name)
					firstvalue = string.split(firstvalue, ":")
					local name = firstvalue[1]
					local number = firstvalue[2]
					local durability = firstvalue[3]
					local image = firstvalue[5]
					local imagetwo = firstvalue[6]
					local slot = current.Name
					slot = current.Name
					local infopaste = name .. ":" .. number .. ":" .. durability .. ":" .. slot .. ":" .. image .. ":" .. imagetwo
					paste(current.Name, infopaste)
					current.Amount.Text = number
					held.Amount.Text = 0
					changeocc(held.Name, false)
					changeocc(current.Name, true)
					local firsttool = player.Backpack:FindFirstChild(name)
					firsttool.INFO.Slot.Value = slot
					held = nil
					current = nil
					update(player)
				else
					local continueorno = holdocc(held)
					if continueorno == true then
						local firstimage = held.Image
						local secondimage = current.Image
						current.Image = firstimage
						held.Image = secondimage
						local firstvalue = copy(held.Name)
						firstvalue = string.split(firstvalue, ":")
						local name = firstvalue[1]
						local number = firstvalue[2]
						local durability = firstvalue[3]
						local image = firstvalue[5]
						local imagetwo = firstvalue[6]
						local slot = current.Name
						local infopaste = name .. ":" .. number .. ":" .. durability .. ":" .. slot .. ":" .. image .. ":" .. imagetwo
						local secondvalue = copy(current.Name)
						secondvalue = string.split(secondvalue, ":")
						local name1 = secondvalue[1]
						local number1 = secondvalue[2]
						local durability1 = secondvalue[3]
						local image1 = secondvalue[5]
						local imagetwo1 = secondvalue[6]
						local slot1 = held.Name
						local infopaste1 = name1 .. ":" .. number1 .. ":" .. durability1 .. ":" .. slot1 .. ":" .. image1 .. ":" .. imagetwo1
						paste(current.Name, infopaste)
						paste(held.Name, infopaste1)
						local firsttool = player.Backpack:FindFirstChild(name)
						local secondtool = player.Backpack:FindFirstChild(name1)
						firsttool.INFO.Slot.Value = slot
						secondtool.INFO.Slot.Value = slot1
						current.Amount.Text = number
						held.Amount.Text = number1
						held = nil
						current = nil
						update(player)
					end
				end
			end
		end
	end
end
local canuse = false
function check(item, needed)
	for slot, occ in pairs(inv) do
		if occ ~= false then
			local firstvalue = occ
			firstvalue = string.split(firstvalue, ":")
			local name = firstvalue[1]
			local number = firstvalue[2]
			local durability = firstvalue[3]
			local imageone = firstvalue[5]
			local imagetwo = firstvalue[6]
			local slot = firstvalue[4]
			local image = imageone .. ":" .. imagetwo
			if name == item then
				ammount = ammount + number
			end
		end
	end
	if ammount >= needed then
		canuse = true
	else
		canuse = false
	end
end
function returnvalue(player)
	ammount = 0
	return canuse
end
local looptable = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twentyone", "twentytwo", "twentythree", "twentyfour", "twentyfive", "twentysix", "twentyseven", "twentyeight", "twentynine", "thirty", "thirtyone", "thirtytwo", "thirtythree", "thirtyfour", "thirtyfive", "thirtysix", "thirtyseven", "thirtyeight", "thirtynine", "fourty", "fourtyone", "fourtytwo", "fourtythree", "fourtyfour", "fourtyfive", "fourtysix", "fourtysevens", "fourtyeight", "fourtynine","fifty", "fiftyone", "fiftytwo", "fiftythree", "fiftyfour", "fiftyfive", "fiftysix", "fiftyseven"}
function findopenslot()
	for slot = #looptable, 1, -1 do
		local occ = looptable[slot]
		local compart = inv[occ]
		if compart == false then
			return occ
		end
	end
	return false
end
function findtool(name, slot)
	for i,v in pairs(plr.Backpack:GetDescendants()) do
		if v.Name == "Slot" then
			if v.Value == slot then
				return v.Parent
			end
		end
	end
	return false
end
function additem(item, add, maxstack)
	local foundaslot = false
	local remain = add
	local finditem = game.ReplicatedStorage.Items:FindFirstChild(item)
	repeat
		wait()
		foundaslot = false
		for slot, occ in pairs(inv) do
			if occ ~= false then
				local firstvalue = occ
				firstvalue = string.split(firstvalue, ":")
				local name = firstvalue[1]
				local number = firstvalue[2]
				local durability = firstvalue[3]
				local imageone = firstvalue[5]
				local imagetwo = firstvalue[6]
				local image = imageone .. ":" .. imagetwo
				local gui = plr.PlayerGui.Inv.Main:FindFirstChild(slot)
				local number1 = tonumber(number)
				if name == item and number1 <= maxstack then
					local test = maxstack - number
					if test < maxstack and test ~= 0 then
						if remain > test then
							remain = remain - test
							number = maxstack
							foundaslot = true
							local infopaste = name .. ":" .. number .. ":" .. durability .. ":" .. slot .. ":" .. image .. ":" .. imagetwo
							paste(slot, infopaste)
							changeocc(slot, true)
							gui.Image = image
							local clone = plr.Backpack:FindFirstChild(item)
							clone.INFO.Slot.Value = slot
							clone.INFO.Ammount.Value = number
						else
							number = number + remain
							remain = 0
							foundaslot = false
							local infopaste = name .. ":" .. number .. ":" .. durability .. ":" .. slot .. ":" .. image .. ":" .. imagetwo
							paste(slot, infopaste)
							changeocc(slot, true)
							gui.Image = image
							local clone = plr.Backpack:FindFirstChild(item)
							clone.INFO.Slot.Value = slot
							clone.INFO.Ammount.Value = number
						end
					end
				end
			end
		end
	until foundaslot == false
	if remain > 0 then
		local name = item
		local number 
		local durability = finditem.INFO.CurDurability.Value .. "/" .. finditem.INFO.MaxDurability.Value
		local image = finditem.INFO.Image.Value
		local found = findopenslot()
		local gui = plr.PlayerGui.Inv.Main:FindFirstChild(found)
		if found ~= false then
			repeat
				if remain > maxstack then
					remain = remain - maxstack
					number = maxstack
					local infopaste = name .. ":" .. number .. ":" .. durability .. ":" .. found .. ":" .. image 
					paste(found, infopaste)
					changeocc(found, true)
					gui.Image = image
					local target = game.ReplicatedStorage.Items:FindFirstChild(item)
					local clone = target:Clone()
					clone.INFO.Slot.Value = found
					clone.INFO.Ammount.Value = number
					clone.Parent = plr.Backpack
				else
					number = remain
					remain = 0
					local infopaste = name .. ":" .. number .. ":" .. durability .. ":" .. found .. ":" .. image
					paste(found, infopaste)
					changeocc(found, true)
					gui.Image = image
					local target = game.ReplicatedStorage.Items:FindFirstChild(item)
					local clone = target:Clone()
					clone.INFO.Slot.Value = found
					clone.INFO.Ammount.Value = number
					clone.Parent = plr.Backpack
				end 
			until remain <= 0
		end
	end
	update(plr)
end
function take(player,item, takeaway)
	local left = takeaway
	repeat
		wait()
		for slot, occ in pairs(inv) do
			if occ ~= false then
				local firstvalue = occ
				firstvalue = string.split(firstvalue, ":")
				local name = firstvalue[1]
				local number = firstvalue[2]
				local durability = firstvalue[3]
				local imageone = firstvalue[5]
				local imagetwo = firstvalue[6]
				local image = imageone .. ":" .. imagetwo
				local number1 = tonumber(number)
				if name == item then
					if left > number1 then
						left = left - number
						paste(slot, false)
					else
						number = number - left
						left = 0
						local infopaste = name .. ":" .. number .. ":" .. durability .. ":" .. slot .. ":" .. image
						paste(slot, infopaste)
					end
				end
			end
		end
	until left <= 0
	update(player)
end
RS.Events.Inventory.REMOVEITEM.OnServerEvent:Connect(function(player,item, takeaway)
	print("good")
	take(player,item, takeaway)
end)
RS.Events.Inventory.ADDITEM.Event:Connect(function(item, add, maxstack)
	additem(item, add, maxstack)
end)
RS.Events.Inventory.CHECK.OnServerInvoke = returnvalue
RS.Events.Inventory.CHECKINFO.OnServerEvent:Connect(function(player, item, needed)
	check(item, needed)
end)
local DOWN = false
down.OnServerEvent:Connect(function(player, guisAtPosition)
	for i = 1, #guisAtPosition do
		local cur = guisAtPosition[i]
		if cur:IsA("ImageLabel") then
			held = cur
			if cur.Image ~= "" then
				RS.Events.Inventory.makevisible:FireClient(player, cur.Image)
			end
		end
	end
end)
up.OnServerEvent:Connect(function(player, guisAtPosition)
	for i = 1, #guisAtPosition do
		local cur = guisAtPosition[i]
		if cur:IsA("ImageLabel") then
			current = cur
			RS.Events.Inventory.makeinvisible:FireClient(player)
		end
	end
	switch(player)
end)
while true do
	wait(5)
	update(plr)
end

equiping script also controls gui (local)

local userinputser = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local visible = true
wait(1)
local current
local Humanoid = player.Character.Humanoid
local looptable = {"SideFrame","Background","ViewportFrame","Frame", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twentyone", "twentytwo", "twentythree", "twentyfour", "twentyfive", "twentysix", "twentyseven", "twentyeight", "twentynine", "thirty", "thirtyone", "thirtytwo", "thirtythree", "thirtyfour", "thirtyfive", "thirtysix", "thirtyseven", "thirtyeight", "thirtynine", "fourty", "fourtyone", "fourtytwo", "fourtythree", "fourtyfour", "fourtyfive", "fourtysix", "fourtyseven", "fourtyeight", "fourtynine","fifty", "fiftyone", "fiftytwo", "fiftythree", "fiftyfour", "fiftyfive", "fiftysix", "fiftyseven"}
function equip(key)
	for i,v in pairs(player.Backpack:GetDescendants()) do
		if v.Name == "Slot" then
			if v.Value == key then
				Humanoid:EquipTool(v.Parent.Parent)
				print(v.Name)
				current = 1
			end
		end
	end
end
function starting(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local PlayerGui = player.PlayerGui
		local pos = input.Position
		local guisAtPosition = PlayerGui:GetGuiObjectsAtPosition(pos.X, pos.Y)
		game.ReplicatedStorage.Events.Inventory.DOWN:FireServer(guisAtPosition)
	elseif input.KeyCode == Enum.KeyCode.E then
		local children = {}
		for i,v in pairs(looptable) do
			local find = script.Parent.Main:FindFirstChild(v)
			table.insert(children, find)
		end
		if visible == true then
			visible = false
			makevisible(false, children)
		else
			visible = true
			makevisible(true, children)
		end
	end
	if input.UserInputType == Enum.UserInputType.Keyboard then --see if the input is from a keyboard
		if input.KeyCode == Enum.KeyCode.One then --if it is from the keyboard check if the key is the key you want
			local key = "one"
			local converted = tostring(key)
			if current ~= nil then
				Humanoid:UnequipTools()
				current = nil
			else
				equip(converted)
			end
		elseif input.KeyCode == Enum.KeyCode.Two then
			local key = "two"
			local converted = tostring(key)
			if current ~= nil then
				Humanoid:UnequipTools()
				current = nil
			else
				equip(converted)
			end
		elseif input.KeyCode == Enum.KeyCode.Three then
			local key = "three"
			local converted = tostring(key)
			if current ~= nil then
				Humanoid:UnequipTools()
				current = nil
			else
				equip(converted)
			end
		elseif input.KeyCode == Enum.KeyCode.Four then
			local key = "four"
			local converted = tostring(key)
			if current ~= nil then
				Humanoid:UnequipTools()
				current = nil
			else
				equip(converted)
			end
		elseif input.KeyCode == Enum.KeyCode.Five then
			local key = "five"
			local converted = tostring(key)
			if current ~= nil then
				Humanoid:UnequipTools()
				current = nil
			else
				equip(converted)
			end
		elseif input.KeyCode == Enum.KeyCode.Six then
			local key = "six"
			local converted = tostring(key)
			if current ~= nil then
				Humanoid:UnequipTools()
				current = nil
			else
				equip(converted)
			end
		elseif input.KeyCode == Enum.KeyCode.Seven then
			local key = "seven"
			local converted = tostring(key)
			if current ~= nil then
				Humanoid:UnequipTools()
				current = nil
			else
				equip(converted)
			end
		end
	end
end
function makevisible(visible, tableuse)
	for i,v in pairs(tableuse) do
		v.Visible = visible
	end
end
function ending(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local PlayerGui = player.PlayerGui
		local pos = input.Position
		local guisAtPosition = PlayerGui:GetGuiObjectsAtPosition(pos.X, pos.Y)
		game.ReplicatedStorage.Events.Inventory.UP:FireServer(guisAtPosition)
	end
end
userinputser.InputBegan:Connect(starting)
userinputser.InputEnded:Connect(ending)
game.ReplicatedStorage.Events.Inventory.makevisible.OnClientEvent:Connect(function(image)
	script.Parent.Main.Show.Visible = true
	script.Parent.Main.Show.Image = image
end)
game.ReplicatedStorage.Events.Inventory.makeinvisible.OnClientEvent:Connect(function()
	script.Parent.Main.Show.Visible = false
end)
game.ReplicatedStorage.Events.Inventory.ANTI.OnClientEvent:Connect(function(number, image, slot)
	local SLOT = script.Parent.Main:FindFirstChild(slot)
	if SLOT then
		SLOT.Image = image
		SLOT.Amount.Text = number
	end
end)
while true do
	wait()
	local mouseLocation = userinputser:GetMouseLocation()
	script.Parent.Main.Show.Position = UDim2.fromOffset(mouseLocation.X - 35, mouseLocation.Y - 35)
end

game fileinventory.rbxl (45.3 KB)

1 Like

Pretty good, just try to limit the while loops, I noticed you had a for loop inside a while true do which if you think about it, could cause heaps of lag depending on what’s inside that for loop. Just be careful with that. But other than that, seems pretty good. Well done.

1 Like