0% found this document useful (0 votes)
58 views18 pages

Roblox Inventory Management Script

This document is a Lua script for a game that manages player inventory and tool interactions. It defines various input controls for equipping, dropping, and moving tools, as well as functions for adding, removing, and updating tools in the player's hotbar and inventory. The script also includes logic for toggling inventory visibility and handling equipped tool displays based on inventory changes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views18 pages

Roblox Inventory Management Script

This document is a Lua script for a game that manages player inventory and tool interactions. It defines various input controls for equipping, dropping, and moving tools, as well as functions for adding, removing, and updating tools in the player's hotbar and inventory. The script also includes logic for toggling inventory visibility and handling equipped tool displays based on inventory changes.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

-- Services

local Players = game:GetService("Players")


local StarterGui = game:GetService("StarterGui")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Objects
local INPUTS_TO_DROP_TOOL = {[Link]}
local INPUTS_TO_GRAB_AND_DROP_ITEM = {[Link].MouseButton1,
[Link]}
local INPUTS_TO_MOVE_GRABED_ITEM = {[Link],
[Link]}
local INPUTS_TO_TOGGLE_INVENTORY_VISIBILITY = {[Link],
[Link].Y} -- Y is temp
local BUTTONS_TO_TOGGLE_INVENTORY_VISIBILITY =
{[Link]} -- text buttons or image
buttons

local HOT_BAR_INPUTS = {
[1] = {[Link]},
[2] = {[Link]},
[3] = {[Link]},
[4] = {[Link]},
[5] = {[Link]},
[6] = {[Link]},
[7] = {[Link]},
[8] = {[Link]},
[9] = {[Link]},
}
local PIXEL_DELAY_TO_DRAG_ITEM = 30
local REPLY_BACKPACK_DELAY_TIME = 0.5
local DEFAULT_COLOR = [Link](255/255, 255/255, 255/255)
local EQUIPPED_COLOR = [Link](0/255, 0/255, 0/255)

local EventsFolder = [Link]


local LocalPlayer = [Link]
local PlayerGUI = [Link]
local InventoryScreenGUI = [Link]
local HotBarGUI = [Link]
local BackpackGUI = [Link]
local ToolSlot = [Link]

local equipedToolDisplayHandled = true


local equipedToolDisplayNameToHandle = ""
local equipedTool = {
IsEquiped = false,
ToolInfo = {
Name = "",
Quantity = 0,
Icon = ""
},
Tool = nil
}

local hotBarTools = {
[1] = "none",
[2] = "none",
[3] = "none",
[4] = "none",
[5] = "none",
[6] = "none",
[7] = "none",
[8] = "none",
[9] = "none",
}
local playerInventoryTools = {}
local tempSlotsOnHotBar = {}
local tempLockedItems = {} -- not using that, but maybe i'll use later

local activedToolSlotToDrag = nil


local firstClickPosition = nil
local passedPixelDelay = false
local firstTickAfterPassedPixelDelay = 0

local activedToolSlotToDragLastParent = nil


local activedToolSlotToDragLastPosition = nil
local activedToolSlotToDragLastSize = nil
local activedToolSlotToDragLastName = nil

local createdTempSlotWhenIsRemovingItemFromHotBarWhenInventoryIsVisble = nil

local function GetToolInfoFromToolSlotFrame(ToolSlotFrame: Frame)


return {
Name = [Link],
Quantity = [Link] == "" and 1 or
tonumber([Link]),
Icon = [Link]
}
end

local function ItemsEqual(item1, item2)


if not item1 or not item2 then return false end

-- Compare all properties of the items


for key, value in pairs(item1) do
if item2[key] ~= value then
return false
end
end

-- Ensure there are no extra properties in item2


for key, value in pairs(item2) do
if item1[key] == nil then
return false
end
end

return true
end

local function GetToolSlotFrameFromToolInfo(ToolInfo): Frame?


for _, toolSlot in pairs([Link]:GetChildren()) do
if [Link]([Link], "ToolSlot") and "L" ~=
[Link]([Link], -1) then
local ToolSlotInfo = GetToolInfoFromToolSlotFrame(toolSlot)
if ItemsEqual(ToolInfo, ToolSlotInfo) then
return toolSlot
end
end
end
for _, toolSlot in pairs([Link]:GetChildren()) do
if [Link]([Link], "ToolSlot") and "L" ~=
[Link]([Link], -1) then
local ToolSlotInfo = GetToolInfoFromToolSlotFrame(toolSlot)
if ItemsEqual(ToolInfo, ToolSlotInfo) then
return toolSlot
end
end
end

warn("ToolSlot was not found!")


return nil
end

local function AddTool(ToolInfo)


local NewToolSlot = ToolSlot:Clone()

local ToolNumber = ""


local NewToolSlotParent = nil

for slotNumber, tool in ipairs(hotBarTools) do


if tool == "none" then
if [Link] then
local SpectedFrameToFind = slotNumber..[Link].."L"

for _, tempSlot in pairs(tempSlotsOnHotBar) do


if [Link] == SpectedFrameToFind then
tempSlot:Destroy()
[Link] = slotNumber..[Link]

hotBarTools[slotNumber] = ToolInfo
NewToolSlotParent = HotBarGUI
ToolNumber = tostring(slotNumber)
end
end
else
hotBarTools[slotNumber] = ToolInfo
NewToolSlotParent = HotBarGUI
ToolNumber = tostring(slotNumber)
end
break
end
end
if NewToolSlotParent == nil then
NewToolSlotParent = BackpackGUI
end

[Link] = ToolNumber
[Link] = [Link] == 1 and "" or
tostring([Link])
[Link] = [Link]
[Link] = [Link]
[Link] = NewToolSlotParent
end

local function RemoveTool(ToolInfo)


local ToolSlotFrame = GetToolSlotFrameFromToolInfo(ToolInfo)
if ToolSlotFrame then
if "L" ~= [Link]([Link], -1) and
tonumber([Link]([Link], 1, 1)) then -- opened inventory
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = [Link].."L"
[Link](tempSlotsOnHotBar,ToolSlotFrame)
else
ToolSlotFrame:Destroy()
end
end

for slot, tool in pairs(hotBarTools) do


if tool ~= "none" then
if ItemsEqual(tool, ToolInfo) then
hotBarTools[slot] = "none"
end
end
end
end

local function UpdateTool(ToolInfoBefore, ToolInfoAfter)


local ToolSlotFrame = GetToolSlotFrameFromToolInfo(ToolInfoBefore)
if ToolSlotFrame then
[Link] = [Link] == 1 and "" or
tostring([Link])
[Link] = [Link]
[Link] = [Link]
end
end

local function MoveTool(StartPoint: Instance, Destination: Instance, ToolSlotFrame:


Frame)
-- HotBar to Inventory
if StartPoint == [Link] and Destination ==
[Link] then
[Link] = ""

for slot, tool in pairs(hotBarTools) do


if tool ~= "none" then
if ItemsEqual(tool,
GetToolInfoFromToolSlotFrame(ToolSlotFrame)) then
hotBarTools[slot] = "none"
end
end
end

[Link] = [Link]
[Link] = Destination
end

-- Inventory to HotBar
if StartPoint == [Link] and [Link] ==
[Link] then
for key, tempSlot in pairs(tempSlotsOnHotBar) do
if [Link] == [Link] then
tempSlotsOnHotBar[key] = nil
end
end
[Link] = [Link]([Link], 1, -2) -- Removes the
last character
[Link] = [Link]
[Link] = [Link]
[Link] = [Link]
hotBarTools[tonumber([Link])] =
GetToolInfoFromToolSlotFrame(Destination)

ToolSlotFrame:Destroy()
end

-- HotBar to HotBar
if StartPoint == [Link] and [Link] ==
[Link] then
for key, tempSlot in pairs(tempSlotsOnHotBar) do
if [Link] == [Link] then
tempSlotsOnHotBar[key] = nil
end
end
hotBarTools[tonumber([Link])] = "none"
[Link] = [Link]([Link], 1, -2)
[Link] = [Link]
[Link] = [Link]
[Link] = [Link]
hotBarTools[tonumber([Link])] =
GetToolInfoFromToolSlotFrame(Destination)

ToolSlotFrame:Destroy()
end

-- Inventory to Inventory
if StartPoint == [Link] and Destination ==
[Link] then
return false
end

return true
end

local function ToolsToVarInfos(Tools: {Tool}) -- may I change this name later


local BackpackTools = {}
for _, tool in pairs(Tools) do
if BackpackTools[[Link]] then
BackpackTools[[Link]].Quantity += 1
else
BackpackTools[[Link]] = {
Name = [Link],
Quantity = 1,
Icon = [Link]
}
end
end

return BackpackTools
end
local function GetBackpackTools()
local BackpackTools = {}

for _, tool in pairs([Link]:GetChildren()) do


if tool:IsA("Tool") then
[Link](BackpackTools, tool)
end
end

return ToolsToVarInfos(BackpackTools)
end

local function IsInventoriesEqual(InventoryA, InventoryB)


if #InventoryA ~= #InventoryB then
return false
end

for toolName, toolInfo in pairs(InventoryA) do


if not InventoryB[toolName] then
return false
else
for key, value in pairs(toolInfo) do
if value ~= InventoryB[toolName][key] then
return false
end
end
end
end

return true
end

local function GetDiffBetweenInventories(MainInventory, TargetInventory)


local diff = {
missing = {},
added = {},
changed = {}
}

-- Check for missing items (in Main but not in Target)


for itemName, itemData in pairs(MainInventory) do
if not TargetInventory[itemName] then
[Link][itemName] = itemData
end
end

-- Check for added items (in Target but not in Main)


for itemName, itemData in pairs(TargetInventory) do
if not MainInventory[itemName] then
[Link][itemName] = itemData
end
end

-- Check for changed items (exist in both but differ)


for itemName, mainItem in pairs(MainInventory) do
local targetItem = TargetInventory[itemName]
if targetItem and not ItemsEqual(mainItem, targetItem) then
[Link][itemName] = {
before = mainItem,
after = targetItem
}
end
end

return diff
end

local function TemporaryRenameItemsOnHotBar()


for _, slot in pairs([Link]:GetChildren()) do
if slot:IsA("Frame") and [Link] == "ToolSlot" then
[Link] = [Link].."ToolSlot"
end
end
end

local function RemoveTemporaryRenameItemsOnHotBar()


for _, slot in pairs([Link]:GetChildren()) do
if slot:IsA("Frame") and [Link]([Link], "ToolSlot") then
[Link] = "ToolSlot"
end
end
end

local function DeleteTempSlotsInHotBarToDisableInventory()


for _, tempSlot in pairs(tempSlotsOnHotBar) do
tempSlot:Destroy()
end

end

local function CreateTempSlot(slotNumber)


local TempSlot = ToolSlot:Clone()

--[[To block moviment, becouse, the name MUST be ToolSlot to be able to


movement, if doesnt, will not be able to movement ]]
[Link] = tostring(slotNumber)..[Link].."L"

[Link] = ""
[Link] = tostring(slotNumber)
[Link] = ""
[Link] = ""

[Link](tempSlotsOnHotBar, TempSlot)
[Link] = [Link]
return TempSlot
end

local function PrepareHotBarToShowInventory()


DeleteTempSlotsInHotBarToDisableInventory()

for slot, tool in pairs(hotBarTools) do


if tool == "none" then
CreateTempSlot(slot)
end
end
end

local function ToggleInventoryVisibility(ActionName, InputState, InputObject)


if InputState ~= [Link] then return end

if [Link] == false then -- Enabling Inventory


TemporaryRenameItemsOnHotBar()
PrepareHotBarToShowInventory()
[Link] = true
else -- Disabling Inventory
RemoveTemporaryRenameItemsOnHotBar()
DeleteTempSlotsInHotBarToDisableInventory()
[Link] = false
end
end

local function HandleEquipedToolsDisplay(Differences)


if not equipedToolDisplayHandled then
equipedToolDisplayHandled = true

local found = false

if [Link] then -- equip


for key, tool in pairs([Link]) do
if [Link] == equipedToolDisplayNameToHandle then
found = true
local ToolSlotEquippedFrame =
GetToolSlotFrameFromToolInfo(tool)
[Link].ImageColor3 =
EQUIPPED_COLOR

[Link][key] = nil
end
end

if not found then

for key, tool in pairs([Link]) do


if [Link] == equipedToolDisplayNameToHandle
then
found = true

local ToolSlotEquippedFrame =
GetToolSlotFrameFromToolInfo([Link])
[Link].ImageColor3 =
EQUIPPED_COLOR

[Link][key] = nil
end
end
end

else -- unequip
for key, tool in pairs([Link]) do
if [Link] == equipedToolDisplayNameToHandle then
found = true
local ToolSlotEquippedFrame =
GetToolSlotFrameFromToolInfo(tool)
[Link].ImageColor3 =
DEFAULT_COLOR

[Link][key] = nil
end
end

if not found then


for key, tool in pairs([Link]) do
if [Link] == equipedToolDisplayNameToHandle
then
found = true

local ToolSlotEquippedFrame =
GetToolSlotFrameFromToolInfo([Link])
[Link].ImageColor3 =
DEFAULT_COLOR

[Link][key] = nil
end
end
end
end

equipedToolDisplayNameToHandle = ""
end
end

local function isItemEquippedEquipped(itemEquiped)


if not [Link] then
return false
end

local character = [Link]


if not character then
return false
end

-- Verifica se o item existe no Character usando o nome salvo


local toolInCharacter = character:FindFirstChild([Link])

-- Verifica também a referência direta do tool (caso o nome tenha sido


alterado)
local toolInstanceValid = [Link] and [Link] ==
character

return toolInCharacter ~= nil or toolInstanceValid


end

local function DeleteEquippedTool()


local ToolInfo = [Link]
local RemoveFromHotBar = true
local ToolSlotFrameDisplay = GetToolSlotFrameFromToolInfo(ToolInfo)
--print([Link])
if [Link] == 1 then
ToolSlotFrameDisplay:Destroy()
else
RemoveFromHotBar = false

[Link] =
tonumber([Link]) - 1

-- update quantity on hotbar


for slot, tool in pairs(hotBarTools) do
if tool ~= "none" then
if ItemsEqual(tool, ToolInfo) then
hotBarTools[slot].Quantity -= 1
end
end
end

if tonumber([Link]) == 1 then
[Link] = ""
end
[Link].ImageColor3 = DEFAULT_COLOR
end

equipedToolDisplayHandled = true
equipedToolDisplayNameToHandle = ""--[Link]

[Link] = false
[Link] = nil
[Link] = {
Name = "",
Quantity = 0,
Icon = ""
}

if RemoveFromHotBar then
for slot, tool in pairs(hotBarTools) do
if tool ~= "none" then
--print(tool, ToolInfo)
if ItemsEqual(tool, ToolInfo) then
hotBarTools[slot] = "none"
end
end
end
end

end

local function HandleEquipedDeletedItems()


local itemIsActuallyEquipped = isItemEquippedEquipped(equipedTool)
local systemThinksEquipped = [Link]

local problem
if not itemIsActuallyEquipped and systemThinksEquipped then
problem = true
else
problem = false
end

if problem then
DeleteEquippedTool()
end
--print(problem)
end

-- add later equip items to not disapear


local function ReplyBackpack()
local BackpackTools = GetBackpackTools()
local Differences = GetDiffBetweenInventories(playerInventoryTools,
BackpackTools)

-- handle equiped tools


HandleEquipedToolsDisplay(Differences)

-- handle changed tools


for _, tool in pairs([Link]) do
UpdateTool([Link], [Link])
end

-- handle new tools


for _, tool in pairs([Link]) do
AddTool(tool)
end

-- handle remove tools


for _, tool in pairs([Link]) do
RemoveTool(tool)
end

HandleEquipedDeletedItems()

playerInventoryTools = BackpackTools
end

local function GetSlotToolFrameBySlotNumber(SlotNumber: IntValue): Frame?


for _, toolSlot in pairs([Link]:GetChildren()) do
if [Link]([Link], "ToolSlot") and "L" ~=
[Link]([Link], -1) then
if tonumber([Link]) == SlotNumber then
return toolSlot
end
end
end
return nil
end

local function GetToolByToolInfo(ToolName:string): Tool?


return [Link]:FindFirstChild(ToolName)
end

local function IsToHandleActivated(ActionName, InputState, InputObject): boolean


if InputState ~= [Link] then return false end
local SlotNumber = tonumber([Link](ActionName, -1))
if not SlotNumber then return false end
return true
end

local function HandleEquip(SlotNumber, DirectTool)

local ToolSlotToActive
if DirectTool then
ToolSlotToActive = DirectTool
else
ToolSlotToActive = GetSlotToolFrameBySlotNumber(SlotNumber)
end
if not ToolSlotToActive then return end

local ToolToEquip = GetToolByToolInfo([Link])


if not ToolToEquip then return end

local LocalPlayerCharacter = [Link]


if LocalPlayerCharacter then
local LocalPlayerHumanoid =
LocalPlayerCharacter:FindFirstChild("Humanoid") or
LocalPlayerCharacter:FindFirstChildOfClass("Humanoid")
if LocalPlayerHumanoid then

equipedToolDisplayHandled = false
equipedToolDisplayNameToHandle = [Link]

[Link] = true
[Link] = ToolToEquip
[Link] =
GetToolInfoFromToolSlotFrame(ToolSlotToActive)

LocalPlayerHumanoid:EquipTool(ToolToEquip)
end
end
end

local function HandleUnequip()


local LocalPlayerCharacter = [Link]
if LocalPlayerCharacter then
local LocalPlayerHumanoid =
LocalPlayerCharacter:FindFirstChild("Humanoid") or
LocalPlayerCharacter:FindFirstChildOfClass("Humanoid")
if LocalPlayerHumanoid then

equipedToolDisplayHandled = false
equipedToolDisplayNameToHandle = [Link]

[Link] = false
[Link] = nil
[Link] = {
Name = "",
Quantity = 0,
Icon = ""
}

LocalPlayerHumanoid:UnequipTools()
end
end
end

local function HandleActivated(ActionName, InputState, InputObject, ClickedTool)


if ClickedTool then -- mouse clicked
if not [Link] then -- Equip
HandleEquip(nil, ClickedTool)
elseif [Link] and [Link] ==
[Link] then -- Unequip
HandleUnequip()
end

else -- number clicked


if not IsToHandleActivated(ActionName, InputState, InputObject) then
return end
local SlotNumber = tonumber([Link](ActionName, -1))
local ToolSlotFrameInSlotNumber =
GetSlotToolFrameBySlotNumber(SlotNumber)

if not ToolSlotFrameInSlotNumber then return end

if not [Link] then -- Equip


HandleEquip(SlotNumber)
elseif [Link] and [Link] ==
[Link] then -- Unequip
HandleUnequip()
end
end
end

local function StartDragItem(InputObject: InputObject, gameProcessedEvent:


BoolValue)
--if gameProcessedEvent then return end
local IsKeyToGrab = false
if activedToolSlotToDrag then return end
for _, key in pairs(INPUTS_TO_GRAB_AND_DROP_ITEM) do
if key == [Link] then
IsKeyToGrab = true
break
end
end
if not IsKeyToGrab then
return
end

local InputPosition = [Link]


local ObjectsBehindInputPosition =
PlayerGUI:GetGuiObjectsAtPosition(InputPosition.X, InputPosition.Y)
for _, object in pairs(ObjectsBehindInputPosition) do
local FormatedObjectName = [Link]([Link], 2)
if FormatedObjectName == [Link] or [Link] == [Link]
then

firstClickPosition = InputPosition
activedToolSlotToDrag = object

return
end
end
end

local function IsToMoveItem(InputObject: InputObject, gameProcessedEvent:


BoolValue)
--if gameProcessedEvent then return false end
if not activedToolSlotToDrag then return false end
local IsKeyToMoveGrabedItem = false
for _, key in pairs(INPUTS_TO_MOVE_GRABED_ITEM) do
if key == [Link] then
IsKeyToMoveGrabedItem = true
break
end
end
if not IsKeyToMoveGrabedItem then
return false
end

return true
end

local function IsInputPositionIsOutsideThePixelDelayToDragItem(InputPosition)


return
InputPosition.X-firstClickPosition.X > PIXEL_DELAY_TO_DRAG_ITEM or
InputPosition.X-firstClickPosition.X < -PIXEL_DELAY_TO_DRAG_ITEM or
InputPosition.Y-firstClickPosition.Y > PIXEL_DELAY_TO_DRAG_ITEM or
InputPosition.Y-firstClickPosition.Y < -PIXEL_DELAY_TO_DRAG_ITEM
end

local function PrepareItemToStartToDrag()

activedToolSlotToDragLastParent = [Link]
activedToolSlotToDragLastPosition = [Link]
activedToolSlotToDragLastSize = [Link]
activedToolSlotToDragLastName = [Link]

local IsRemovingItemFromHotBarWhenInventoryIsVisble =
tonumber([Link]([Link], 1, 1)) and
[Link] == [Link] and
[Link]

if IsRemovingItemFromHotBarWhenInventoryIsVisble then
createdTempSlotWhenIsRemovingItemFromHotBarWhenInventoryIsVisble =
CreateTempSlot([Link]([Link], 1, 1))
end

local ObjectAbsoluteSize = [Link](0,[Link].X,


0, [Link].Y)
local ObjectAbsolutePosition =
[Link](0,[Link].X, 0,
[Link].Y)
local FormatedObjectAbsolutePosition =
[Link](0,[Link].X, 0,
[Link].Y+[Link])
[Link] = InventoryScreenGUI
[Link] = FormatedObjectAbsolutePosition
[Link] = ObjectAbsoluteSize

end

local function MoveDragItem(InputObject: InputObject, gameProcessedEvent:


BoolValue)

if not IsToMoveItem(InputObject, gameProcessedEvent) then return end

local InputPosition = [Link]


if IsInputPositionIsOutsideThePixelDelayToDragItem(InputPosition) then
passedPixelDelay = true
firstTickAfterPassedPixelDelay += 1
end

if firstTickAfterPassedPixelDelay == 1 then
PrepareItemToStartToDrag()
end

if passedPixelDelay then
local CellSize = [Link]
local NewToolSlotPositionX = InputPosition.X-(CellSize.X/2)
local NewToolSlotPositionY = InputPosition.Y+(CellSize.Y/2)
local NewToolSlotPositionOffset = [Link](0, NewToolSlotPositionX, 0,
NewToolSlotPositionY)
[Link] = NewToolSlotPositionOffset
end
end

local function IsToDropItem(InputObject: InputObject, gameProcessedEvent:


BoolValue)
--if gameProcessedEvent then return false end
if not activedToolSlotToDrag then return false end
if not passedPixelDelay then return false end
local IsKeyToDrop = false
for _, key in pairs(INPUTS_TO_GRAB_AND_DROP_ITEM) do
if key == [Link] then
IsKeyToDrop = true
break
end
end
if not IsKeyToDrop then
return false
end

return true
end

local function ResetValuesAfterDrag()


activedToolSlotToDrag = nil
passedPixelDelay = false
firstClickPosition = nil
firstTickAfterPassedPixelDelay = 0

activedToolSlotToDragLastParent = nil
activedToolSlotToDragLastPosition = nil
activedToolSlotToDragLastSize = nil
activedToolSlotToDragLastName = nil

createdTempSlotWhenIsRemovingItemFromHotBarWhenInventoryIsVisble = nil
end

local function DropedInInventoryFrame(Object)


return [Link] == "Frame" and [Link] ==
[Link]
end

local function DropedInHotBarTempSlot(Object)


return [Link]([Link], ([Link].."L")) and
tonumber([Link]([Link], 1, 1))
end

local function ReturnObjectToLastPosition(Object: Frame)


if createdTempSlotWhenIsRemovingItemFromHotBarWhenInventoryIsVisble then
for _, tempSlot in pairs(tempSlotsOnHotBar) do
if [Link] == [Link].."L" then
tempSlot:Destroy()
end
end
end

[Link] = activedToolSlotToDragLastName
[Link] = activedToolSlotToDragLastSize
[Link] = activedToolSlotToDragLastPosition
[Link] = activedToolSlotToDragLastParent
end

local function DropDragItem(InputObject: InputObject, gameProcessedEvent:


BoolValue)
if not passedPixelDelay and activedToolSlotToDrag then -- clicked
HandleActivated(nil, nil, nil, activedToolSlotToDrag)
else -- dragged
if not IsToDropItem(InputObject, gameProcessedEvent) then return end
local InputPosition = [Link]
local ObjectsBehindInputPosition =
PlayerGUI:GetGuiObjectsAtPosition(InputPosition.X, InputPosition.Y)
local IsToReturnToTheLastPosition = false

local foundObject = false


for _, object in pairs(ObjectsBehindInputPosition) do

if DropedInHotBarTempSlot(object) then
foundObject = true
local sucess = MoveTool(activedToolSlotToDragLastParent,
object, activedToolSlotToDrag)
if not sucess then
IsToReturnToTheLastPosition = true
end
break
elseif DropedInInventoryFrame(object) then
foundObject = true
local sucess = MoveTool(activedToolSlotToDragLastParent,
object, activedToolSlotToDrag)
if not sucess then
IsToReturnToTheLastPosition = true
end
break
end
end

if not foundObject then


IsToReturnToTheLastPosition = true
end
if IsToReturnToTheLastPosition then
ReturnObjectToLastPosition(activedToolSlotToDrag)
end
end

ResetValuesAfterDrag()
end

local function CheckInventoryLoop()


while [Link](REPLY_BACKPACK_DELAY_TIME) do
ReplyBackpack()
end
end

local function DropItem(ActionName, InputState, InputObject)


if InputState ~= [Link] then return end

local Character = [Link]


if not Character then return end

if not [Link] or not [Link] then return end

--print([Link])

[Link]:FireServer([Link], Character)
DeleteEquippedTool()
end

-- Guardar conexões para desconectar depois


local activeConnections = {}

-- Função para limpar conexões antigas


local function ClearConnections()
for _, conn in pairs(activeConnections) do
if conn and [Link] then
conn:Disconnect()
end
end
activeConnections = {}
ContextActionService:UnbindAction("ToggleInventoryVisibility")
for i = 1, 9 do
ContextActionService:UnbindAction("ActiveSlot"..i)
end
ContextActionService:UnbindAction("DropItem")
end

-- Função principal de setup


local function SetupConnections()
ClearConnections()

StarterGui:SetCoreGuiEnabled([Link], false)

[Link](activeConnections,
[Link]:Connect(ReplyBackpack))
[Link](activeConnections,
[Link]:Connect(ReplyBackpack))
[Link](CheckInventoryLoop)

[Link](activeConnections,
[Link]:Connect(StartDragItem))
[Link](activeConnections,
[Link]:Connect(MoveDragItem))
[Link](activeConnections,
[Link]:Connect(DropDragItem))

ContextActionService:BindAction("ToggleInventoryVisibility",
ToggleInventoryVisibility, false, unpack(INPUTS_TO_TOGGLE_INVENTORY_VISIBILITY))

for i = 1, 9 do
ContextActionService:BindAction("ActiveSlot"..i, HandleActivated,
false, unpack(HOT_BAR_INPUTS[i]))
end

ContextActionService:BindAction("DropItem", DropItem, false,


unpack(INPUTS_TO_DROP_TOOL))
end

-- Inicial
SetupConnections()

-- Quando o personagem for recriado, refazer as conexões


[Link]:Connect(function(char)
char:WaitForChild("Humanoid").Died:Connect(function()
-- Aqui você pode reagir à morte se quiser
end)

[Link](0.5)
SetupConnections()
end)

You might also like