Currently, I’m making an inventory system similar to that of Deepwoken’s. I have the hotbar and inventory done but I just need a way to transfer items between them. All I need to do is find a way to make my slots draggable between hotbar slots and inventories, however I can’t find any good way (or code) to do it. Here is my code:
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Hotbar = script.Parent.Hotbar
local inventory = script.Parent.InventoryContainer
local SlotTemplate = script.SlotTemplate
local UIS = game:GetService("UserInputService")
local DB1 = false
local EquipCD = false
local hotbarTable = {}
local inventoryTable = {}
local SlotConnections = {}
local equipped
local dragModule = require(script.DraggableObject)
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
game.StarterGui:SetCoreGuiEnabled(Enum. CoreGuiType. PlayerList, false)
local function findNearestSlot()
local index = 1
repeat
if hotbarTable[index] == nil then
return "Hotbar"..index
else
index += 1
end
until index == 9
if hotbarTable[0] == nil then
return "Hotbar0"
end
return "Inventory"
end
local function getToolFromName(name)
if char:FindFirstChild(name) then return char[name] end
for i,v in pairs(player.Backpack:GetDescendants()) do
if v:IsA("Tool") and v.Name == name then
return v
end
end
end
local function update()
table.clear(hotbarTable)
table.clear(inventoryTable)
for i,v in pairs(script.Parent:GetDescendants()) do
if v.Name == "Slot" then
v:Destroy()
print("clear")
end
end
for i,v in pairs(SlotConnections) do
v:Disconnect()
print("disconnected")
end
equipped = nil
for i,v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
--Initialize and create the slot itself and fill out information
local SlotTemplate = script.SlotTemplate:Clone()
SlotTemplate.Parent = script.Parent.Temp
SlotTemplate:SetAttribute("ItemName",v.Name)
SlotTemplate:SetAttribute("ItemType",v:GetAttribute("Class"))
SlotTemplate.Name = "Slot"
SlotTemplate.Info:FindFirstChild("Name").Text = SlotTemplate:GetAttribute("ItemName")
local handleClone = v:FindFirstChild("Handle"):Clone()
handleClone.Parent = SlotTemplate.ImagePreview
local Camera = Instance.new("Camera")
repeat Camera.CameraType = Enum.CameraType.Scriptable until Camera.CameraType == Enum.CameraType.Scriptable
Camera.Parent = SlotTemplate.ImagePreview
Camera.CFrame = handleClone.CFrame + Vector3.new(0,0,-3)
SlotTemplate.HotbarNumber.Count.Text = string.gsub(v:GetAttribute("InventoryLocation"), "Hotbar","")
SlotTemplate.Count.TextLabel.Text = "x".. v:GetAttribute("Quantity")
SlotTemplate.ImagePreview.CurrentCamera = Camera
if v:GetAttribute("Stars") > 0 then SlotTemplate:WaitForChild("Info").Stars.Visible = true end
local location = v:GetAttribute("InventoryLocation")
if location == nil or location == "" then
location = "Inventory"
v:SetAttribute("InventoryPosition",location)
end
local x = string.gsub(location,"Hotbar","")
if table.find(hotbarTable,tonumber(x)) then
location = findNearestSlot()
v:SetAttribute("InventoryPosition",location)
end
SlotTemplate:SetAttribute("Position",location)
local position = SlotTemplate:GetAttribute("Position")
if string.find(position,"Hotbar") then --check if item belongs in the hotbar and position it
local position = "Container"..string.gsub(position, "Hotbar","")
local numberPosition = (string.gsub(position,"Container",""))
hotbarTable[tonumber(numberPosition)] = SlotTemplate
SlotTemplate.Parent = Hotbar[position]
SlotTemplate.HotbarNumber.Visible = true
elseif position == "Inventory" then --put it in the inventory
SlotTemplate.Parent = script.Parent.InventoryContainer.Inventory
table.insert(inventoryTable,SlotTemplate)
end
v:SetAttribute("InventoryPosition",location)
print("before reached")
local connection = SlotTemplate.Button.MouseButton1Click:Connect(function()
if char:FindFirstChild(SlotTemplate:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
SlotTemplate.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(SlotTemplate:GetAttribute("ItemName"))); equipped = getToolFromName(SlotTemplate:GetAttribute("ItemName"))
SlotTemplate.BackgroundTransparency = 0
end
end)
table.insert(SlotConnections,connection)
v.Changed:Connect(function(property)
if property == "Parent" and DB1 == false then
if v.Parent ~= player.Backpack and v.Parent ~= char then
print("Item Dropped")
DB1 = true
update()
task.delay(1,function()
DB1 = false
end)
end
end
end)
end
end
--for i,v in pairs(script.Parent:GetDescendants()) do
-- print("Hi")
-- if v.Name == "Slot" then
-- print(v)
-- print(v:GetAttribute("Position"))
-- local position = v:GetAttribute("Position")
-- local ItemName = v:GetAttribute("ItemName")
-- local ItemType = v:GetAttribute("ItemType")
-- end
--end
end
UIS.InputBegan:Connect(function(Key,GP)
if GP then return end
if Key.KeyCode == Enum.KeyCode.One then
local hotbarSlot = hotbarTable[1]
if hotbarSlot == nil then return end
if char:FindFirstChild(hotbarSlot:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
hotbarSlot.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(hotbarSlot:GetAttribute("ItemName"))); equipped = getToolFromName(hotbarSlot:GetAttribute("ItemName"))
hotbarSlot.BackgroundTransparency = 0
end
end
if Key.KeyCode == Enum.KeyCode.Two then
local hotbarSlot = hotbarTable[2]
if hotbarSlot == nil then return end
if char:FindFirstChild(hotbarSlot:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
hotbarSlot.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(hotbarSlot:GetAttribute("ItemName"))); equipped = getToolFromName(hotbarSlot:GetAttribute("ItemName"))
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
hotbarSlot.BackgroundTransparency = 0
end
end
if Key.KeyCode == Enum.KeyCode.Three then
local hotbarSlot = hotbarTable[3]
if hotbarSlot == nil then return end
if char:FindFirstChild(hotbarSlot:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
hotbarSlot.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(hotbarSlot:GetAttribute("ItemName"))); equipped = getToolFromName(hotbarSlot:GetAttribute("ItemName"))
hotbarSlot.BackgroundTransparency = 0
end
end
if Key.KeyCode == Enum.KeyCode.Four then
local hotbarSlot = hotbarTable[4]
if hotbarSlot == nil then return end
if char:FindFirstChild(hotbarSlot:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
hotbarSlot.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(hotbarSlot:GetAttribute("ItemName"))); equipped = getToolFromName(hotbarSlot:GetAttribute("ItemName"))
hotbarSlot.BackgroundTransparency = 0
end
end
if Key.KeyCode == Enum.KeyCode.Five then
local hotbarSlot = hotbarTable[5]
if hotbarSlot == nil then return end
if char:FindFirstChild(hotbarSlot:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
hotbarSlot.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(hotbarSlot:GetAttribute("ItemName"))); equipped = getToolFromName(hotbarSlot:GetAttribute("ItemName"))
hotbarSlot.BackgroundTransparency = 0
end
end
if Key.KeyCode == Enum.KeyCode.Six then
local hotbarSlot = hotbarTable[6]
if hotbarSlot == nil then return end
if char:FindFirstChild(hotbarSlot:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
hotbarSlot.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(hotbarSlot:GetAttribute("ItemName"))); equipped = getToolFromName(hotbarSlot:GetAttribute("ItemName"))
hotbarSlot.BackgroundTransparency = 0
end
end
if Key.KeyCode == Enum.KeyCode.Seven then
local hotbarSlot = hotbarTable[7]
if hotbarSlot == nil then return end
if char:FindFirstChild(hotbarSlot:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
hotbarSlot.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(hotbarSlot:GetAttribute("ItemName"))); equipped = getToolFromName(hotbarSlot:GetAttribute("ItemName"))
hotbarSlot.BackgroundTransparency = 0
end
end
if Key.KeyCode == Enum.KeyCode.Eight then
local hotbarSlot = hotbarTable[8]
if hotbarSlot == nil then return end
if char:FindFirstChild(hotbarSlot:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
hotbarSlot.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(hotbarSlot:GetAttribute("ItemName"))); equipped = getToolFromName(hotbarSlot:GetAttribute("ItemName"))
hotbarSlot.BackgroundTransparency = 0
end
end
if Key.KeyCode == Enum.KeyCode.Nine then
local hotbarSlot = hotbarTable[9]
if hotbarSlot == nil then return end
if char:FindFirstChild(hotbarSlot:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
hotbarSlot.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(hotbarSlot:GetAttribute("ItemName"))); equipped = getToolFromName(hotbarSlot:GetAttribute("ItemName"))
hotbarSlot.BackgroundTransparency = 0
end
end
if Key.KeyCode == Enum.KeyCode.Zero then
local hotbarSlot = hotbarTable[0]
if hotbarSlot == nil then return end
if char:FindFirstChild(hotbarSlot:GetAttribute("ItemName")) then
if EquipCD == true then return else EquipCD = true; task.delay(0.5, function() EquipCD = false end) end
char:FindFirstChildOfClass("Humanoid"):UnequipTools(); equipped = nil
hotbarSlot.BackgroundTransparency = 0.5
else
if EquipCD == true then return else EquipCD = true; task.delay(2, function() EquipCD = false end) end
for i,v in pairs(hotbarTable) do
if v~= nil then
v.BackgroundTransparency = 0.5
end
end
char:FindFirstChildOfClass("Humanoid"):EquipTool(player.Backpack:FindFirstChild(hotbarSlot:GetAttribute("ItemName"))); equipped = getToolFromName(hotbarSlot:GetAttribute("ItemName"))
hotbarSlot.BackgroundTransparency = 0
end
end
if Key.KeyCode == Enum.KeyCode.Tab then
if inventory.Visible == false then
inventory.Visible = true
else
inventory.Visible = false
end
end
end)
task.wait(2)
update()
char.ChildAdded:Connect(function(instance)
if not instance:IsA("Tool") then return end
print("---------------")
print(equipped)
print(instance)
print("==============================")
if instance == equipped then return end
instance.Parent = player.Backpack
local handle = instance:FindFirstChild("Handle")
instance:SetAttribute("InventoryLocation",findNearestSlot())
update()
end)
--player.Backpack.ChildRemoved:Connect(function(child)
-- pcall(function(a,b)
-- if char:FindFirstChild(equipped) then return end
-- print("update")
-- update()
-- end)
--end)
Here is my heirarchy:
here is the UI:
For some reason, for all the code I use, the mouse click doesn’t register.
Here is my slot template: