i goot important question. i got this backpack but i cannot drag item frames on mobile because of the tooltips. could you do me a giant favor and remove this for me? i want to be able to drag the itemframes on mobile! if you do this for me i owe you a lot!
----------[[ DECLARE VARIABLES ]]----------
local Player = game:GetService('Players').LocalPlayer
local Backpack = Player:WaitForChild('Backpack')
local RunService = game:GetService('RunService')
local UserInputService = game:GetService('UserInputService')
local BBSettings = game:GetService('ReplicatedStorage'):WaitForChild('BBSettings')
local Settings = {}
for i,v in pairs(BBSettings:GetAttributes()) do -- Adds settings to settings table
Settings[i] = v
end
local Hotbar = script.Parent:WaitForChild('Hotbar')
local Inventory = script.Parent:WaitForChild('Inventory')
local InventoryButton = script.Parent:WaitForChild('InventoryButton')
local CurrentlySelected = nil
----------[[ FUNCTIONS ]]----------
function ToggleBackpack() -- Displays BetterBackpack or not based on if there's anything to display
local FoundTool = false
for i,v in pairs(script.Parent:GetDescendants()) do
if v:IsA('ObjectValue') and v.Name == 'Tool' and v.Value ~= nil then
FoundTool = true
end
end
if FoundTool == false then
script.Parent.Enabled = false
else
script.Parent.Enabled = true
end
end
function AddToolTips(Frame)
local ToolTip
Frame.MouseEnter:Connect(function()
if Settings['ToolTips'] == true then
local Tool = Frame.Tool.Value
if Tool ~= nil and Tool.ToolTip ~= '' then
ToolTip = script.ToolTip:Clone()
ToolTip.Text = Tool.ToolTip
ToolTip.Parent = Frame
if Inventory.Visible then
InventoryButton.Position = UDim2.new(0.5, 0, 1, -355)
end
Inventory.Position = UDim2.new(0.5, 10, 1, -330)
end
end
end)
Frame.MouseLeave:Connect(function()
if ToolTip ~= nil then
ToolTip:Destroy()
ToolTip = nil
if Inventory.Visible then
InventoryButton.Position = UDim2.new(0.5, 0, 1, -330)
else
InventoryButton.Position = UDim2.new(0.5, 0, 1, -115)
end
Inventory.Position = UDim2.new(0.5, 10, 1, -305)
end
end)
end
function ToggleInventoryButton(Status)
if Status == 'InventoryOpen' then -- Show close arrow
InventoryButton.Rotation = 180
InventoryButton.Position = UDim2.new(0.5, 0, 1, -330)
InventoryButton.TextLabel.Visible = false
elseif Status == 'InventoryClosed' then -- Show open arrow
InventoryButton.Rotation = 0
InventoryButton.Position = UDim2.new(0.5, 0, 1, -115)
local InventoryContents = #Inventory.ScrollingFrame:GetChildren() - 2
if InventoryContents <= 0 then
InventoryButton.TextLabel.Text = ''
else
InventoryButton.TextLabel.Text = InventoryContents
end
InventoryButton.TextLabel.Visible = true
end
end
function AddToInventory(Tool)
local CurrentInventorySlots = #Inventory.ScrollingFrame:GetChildren() - 2
if Settings['MaxInventorySlots'] == 0 or CurrentInventorySlots < Settings['MaxInventorySlots'] then
local NewToolFrame = script.ToolFrame:Clone()
NewToolFrame.Name = Tool.Name
NewToolFrame.TextButton.Text = Tool.Name
NewToolFrame.Tool.Value = Tool
NewToolFrame.Parent = Inventory.ScrollingFrame
NewToolFrame.Visible = true
Draggable(NewToolFrame)
AddToolTips(NewToolFrame)
InventoryButton.TextLabel.Text = CurrentInventorySlots + 1
local Rows = math.ceil((CurrentInventorySlots + 1)/10)
Inventory.ScrollingFrame.CanvasSize = UDim2.new(0, 0, 0, (65 * Rows) - 5)
else
warn('Unable to add '..Tool.Name..' to BetterBackpack Inventory. No space available.')
end
end
function AddToBB(Tool)
local Slot
for i = 1, Settings['MaxHotbarSlots'] do
local ToolFrame = Hotbar:FindFirstChild(i)
if ToolFrame and ToolFrame.Tool.Value == nil then
Slot = ToolFrame
break
end
end
if Slot then
Slot.Tool.Value = Tool
Slot.TextButton.Text = Tool.Name
Slot.Visible = true
else
AddToInventory(Tool)
end
end
function RemoveFromBB(Frame)
if Frame.Parent == Hotbar then
Frame.Tool.Value = nil
Frame.TextButton.Text = ''
if not Inventory.Visible then
Frame.Visible = false
end
else
Frame:Destroy()
end
end
function CheckForTool(Tool)
for i,v in pairs(script.Parent:GetDescendants()) do
if v:IsA('ObjectValue') and v.Name == 'Tool' then
if v.Value == Tool then
return true
end
end
end
return false
end
function ToggleHighlight(Frame, Bool)
if Bool == true then
Frame.Background.Size = UDim2.new(0, 50, 0, 50)
Frame.Background.UIStroke.Enabled = true
else
Frame.Background.Size = UDim2.new(0, 60, 0, 60)
Frame.Background.UIStroke.Enabled = false
end
end
function SavePosition(Tool, Slot)
if Settings['SaveToolPositions'] == true then
local Position = tonumber(Slot.TextLabel.Text)
if Position >= 0 and Position <= 9 then
local StringValue = BBSettings:FindFirstChild(Position)
if StringValue then
StringValue.Value = Tool.Name
else
StringValue = Instance.new('StringValue')
StringValue.Name = Position
StringValue.Value = Tool.Name
StringValue.Parent = BBSettings
end
end
end
end
function ToggleEquip(Frame)
if Player.Character then
local Humanoid = Player.Character:FindFirstChildOfClass('Humanoid')
if Humanoid then
if CurrentlySelected ~= nil then
ToggleHighlight(CurrentlySelected, false)
if CurrentlySelected ~= Frame then
Humanoid:EquipTool(Frame.Tool.Value)
ToggleHighlight(Frame, true)
CurrentlySelected = Frame
else
CurrentlySelected = nil
Humanoid:UnequipTools()
end
else
Humanoid:EquipTool(Frame.Tool.Value)
ToggleHighlight(Frame, true)
CurrentlySelected = Frame
end
end
end
end
function Draggable(Frame)
local MouseUpConnections = {}
local PreviousName
local function Disconnect()
for i,v in pairs(MouseUpConnections) do
if v ~= nil and v.Connected then
v:Disconnect()
v = nil
end
end
MouseUpConnections = {}
end
Frame.TextButton.InputBegan:Connect(function(InputObject)
if Frame.Tool.Value ~= nil and (InputObject.UserInputType == Enum.UserInputType.MouseButton1 or InputObject.UserInputType == Enum.UserInputType.Touch) then -- Don't allow anything if it's empty
local DragFrame
table.insert(MouseUpConnections, UserInputService.InputChanged:Connect(function(NewInput) -- Input changed
if NewInput.UserInputType == Enum.UserInputType.MouseMovement or NewInput.UserInputType == Enum.UserInputType.Touch then
if DragFrame == nil then -- Player isn't dragging, lets run some tests to see if the player is trying to drag
local Distance = (InputObject.Position - NewInput.Position).Magnitude
if Distance >= 30 then -- Let's not drag the frame unless the player holds the mouse and pulls far enough away
PreviousName = Frame.TextButton.Text
DragFrame = Frame:Clone()
Frame.TextButton.Text = ''
DragFrame.TextLabel.Text = ''
DragFrame.Position = UDim2.new(0, InputObject.Position.X - DragFrame.Size.X.Offset/2, 0, InputObject.Position.Y - DragFrame.Size.Y.Offset/2)
ToggleHighlight(DragFrame, true)
DragFrame.Parent = script.Parent
end
else -- Player is already dragging, update dragframe to input position
DragFrame.Position = UDim2.new(0, NewInput.Position.X - DragFrame.Size.X.Offset/2, 0, NewInput.Position.Y - DragFrame.Size.Y.Offset/2)
end
end
end))
table.insert(MouseUpConnections, InputObject.Changed:Connect(function()
if InputObject.UserInputState == Enum.UserInputState.End then -- Player is no longer clicking
Disconnect() -- Disconnect the mouseup connections
if DragFrame ~= nil then -- No dragframe? Player wasnt dragging a frame then!
DragFrame:Destroy() -- Oh, it was dragging a frame? Not anymore...
DragFrame = nil
local Slot
local Guis = Player.PlayerGui:GetGuiObjectsAtPosition(InputObject.Position.X, InputObject.Position.Y) -- Lets see if there was any slots where the dragframe was dropped
for i,v in pairs(Guis) do
if v.Parent == Hotbar or v.Parent == Inventory.ScrollingFrame or v == Inventory.ScrollingFrame then
Slot = v
break
end
end
if Slot then -- Was there a slot where the dragframe was dropped?
if Slot == Inventory.ScrollingFrame and Frame.Parent == Inventory.ScrollingFrame then -- Player attempted to move Slot back into their inventory, return
Frame.TextButton.Text = PreviousName
else -- Player dropped dragframe literally anywhere besides inventory to inventory
if Frame == CurrentlySelected then -- Did the dragframe originate from a tool currently equipped?
ToggleHighlight(Frame, false)
if Slot ~= Inventory.ScrollingFrame and Slot.Parent ~= Inventory.ScrollingFrame then -- Making sure player is not trying to drop frame into inventory
ToggleHighlight(Slot, true)
CurrentlySelected = Slot
else -- Player wants to drop frame into inventory, unselect and unequip
if Player.Character then
local Humanoid = Player.Character:FindFirstChildOfClass('Humanoid')
if Humanoid then
CurrentlySelected = nil
Humanoid:UnequipTools()
end
end
end
end
if Slot == Inventory.ScrollingFrame and Frame.Parent == Hotbar then -- Dropping dragrame from Hotbar -> Inventory
local CurrentInventorySlots = #Inventory.ScrollingFrame:GetChildren() - 2
if Settings['MaxInventorySlots'] == 0 or CurrentInventorySlots < Settings['MaxInventorySlots'] then -- Make sure inventory isn't full
print('Clear '..Frame.Tool.Value.Name..' from '..Frame.TextLabel.Text)
local SavedPosition = BBSettings:FindFirstChild(Frame.TextLabel.Text)
if SavedPosition then
SavedPosition:Destroy()
end
AddToInventory(Frame.Tool.Value)
Frame.Tool.Value = nil
Frame.TextButton.Text = ''
else -- Uh oh, inventory is full. Return slot
warn('Inventory full')
Frame.TextButton.Text = PreviousName
end
elseif Slot.Parent == Hotbar and Frame.Parent == Inventory.ScrollingFrame then -- Dropping dragframe from Inventory -> Hotbar
local ToolSwapping = Slot.Tool.Value
Slot.Tool.Value = Frame.Tool.Value
Slot.TextButton.Text = Frame.Tool.Value.Name
print('[Moved From Inventory to Hotbar] Update '..Slot.Tool.Value.Name..' to '..Slot.TextLabel.Text)
SavePosition(Slot.Tool.Value, Slot)
if ToolSwapping == nil then
Frame:Destroy()
else
Frame.Tool.Value = ToolSwapping
Frame.TextButton.Text = ToolSwapping.Name
end
local CurrentInventorySlots = #Inventory.ScrollingFrame:GetChildren() - 2 -- Update scrollingframe size
local Rows = math.ceil((CurrentInventorySlots + 1)/10)
Inventory.ScrollingFrame.CanvasSize = UDim2.new(0, 0, 0, (65 * Rows) - 5)
elseif Slot:FindFirstChild('Tool') then -- Player is attempting to trade slots with another existing tool, usually hotbar -> hotbar or hotbar -> existing inventory slot
local ToolSwapping = Slot.Tool.Value
Slot.Tool.Value = Frame.Tool.Value
Slot.TextButton.Text = Frame.Tool.Value.Name
if Slot.Parent == Hotbar then
print('[Moved to Hotbar] Update '..Slot.Tool.Value.Name..' to '..Slot.TextLabel.Text)
SavePosition(Slot.Tool.Value, Slot)
end
if ToolSwapping ~= nil then -- Swapping tools
Frame.Tool.Value = ToolSwapping
Frame.TextButton.Text = ToolSwapping.Name
if Frame.Parent == Hotbar then
print('[Swapped] Update '..Frame.Tool.Value.Name..' to '..Frame.TextLabel.Text)
SavePosition(Frame.Tool.Value, Frame)
end
else -- This should only happen when player is attempting to swap spots on the hotbar with a slot that doesn't have a tool on it
if Frame.Parent == Hotbar then
print('Clear '..Frame.Tool.Value.Name..' from '..Frame.TextLabel.Text)
local SavedPosition = BBSettings:FindFirstChild(Frame.TextLabel.Text)
if SavedPosition then
SavedPosition:Destroy()
end
end
Frame.Tool.Value = nil
Frame.TextButton.Text = ''
end
end
end
else -- There was no slot where dragframe was dropped, return slot
Frame.TextButton.Text = PreviousName
end
else -- No dragframe? Is just a click!
if Frame.Tool.Value ~= nil then -- Is there a tool there?
ToggleEquip(Frame)
end
end
end
end))
end
end)
end
----------[[ INVENTORY ]]----------
local ToggleInventoryEvent
local ToggleInventoryButtonEvent
function ToggleInventory(Bool)
local function Toggle()
Inventory.Visible = not Inventory.Visible
if Inventory.Visible then -- Inventory is open
ToggleInventoryButton('InventoryOpen')
for i,v in pairs(Hotbar:GetChildren()) do
if v:IsA('Frame') then
v.Visible = true
end
end
else -- Inventory is closed
ToggleInventoryButton('InventoryClosed')
for i,v in pairs(Hotbar:GetChildren()) do
if v:IsA('Frame') and v.TextButton.Text == '' then
v.Visible = false
end
end
end
end
if Bool == true then
InventoryButton.Visible = true
ToggleInventoryEvent = UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
if InputObject.KeyCode == Enum.KeyCode.Backquote and not GameProcessed then
Toggle()
end
end)
ToggleInventoryButtonEvent = InventoryButton.Activated:Connect(function()
Toggle()
end)
elseif Bool == false then
InventoryButton.Visible = false
if ToggleInventoryEvent ~= nil then
ToggleInventoryEvent:Disconnect()
ToggleInventoryEvent = nil
end
if ToggleInventoryButtonEvent ~= nil then
ToggleInventoryButtonEvent:Disconnect()
ToggleInventoryButtonEvent = nil
end
end
end
if Settings['BackpackEnabled'] == true then
ToggleInventory(Settings['InventoryEnabled'])
end
----------[[ HOTBAR ]]----------
if Settings['MaxHotbarSlots'] <= 0 then -- Fixing any developer mistakes before it becomes an issue
Settings['MaxHotbarSlots'] = 0
BBSettings:SetAttribute('MaxHotbarSlots', 0)
warn('BetterBackpack: Developer set MaxHotbarSlots to '..Settings['MaxHotbarSlots']..'. BetterBackpack has been disabled.')
script.Parent.Enabled = false
else
if Settings['MaxHotbarSlots'] > 10 then
warn('BetterBackpack: Developer attempted to exceed 10 hotbar slots. Only 10 slots will display.')
Settings['MaxHotbarSlots'] = 10
BBSettings:SetAttribute('MaxHotbarSlots', 10)
end
script.Parent.Enabled = true
end
for i = 1, Settings['MaxHotbarSlots'] do -- Setup hotbar slots
local DisplayNum = i
if DisplayNum == 10 then
DisplayNum = 0
end
local NewToolFrame = script.ToolFrame:Clone()
NewToolFrame.Name = i
NewToolFrame.TextLabel.Text = DisplayNum
NewToolFrame.LayoutOrder = i
NewToolFrame.Parent = Hotbar
Draggable(NewToolFrame)
AddToolTips(NewToolFrame)
end
for i,v in pairs(Backpack:GetChildren()) do -- Deal with stuff already in backpack before script loaded
AddToBB(v)
end
ToggleBackpack() -- Displays BetterBackpack or not based on if there's anything to display
Backpack.ChildAdded:Connect(function(Child)
if Child:IsA('Tool') then
if not CheckForTool(Child) then
AddToBB(Child)
ToggleBackpack() -- Displays BetterBackpack or not based on if there's anything to display
end
end
end)
Backpack.ChildRemoved:Connect(function(Child)
if Child:IsA('Tool') then
local EquippedTool = Player.Character and Player.Character:FindFirstChild(Child.Name)
if not EquippedTool then -- If it's equipped, leave it alone!
for i,v in pairs(script.Parent:GetDescendants()) do -- Wasn't equipped, lets get rid of any slots with this tool
if v:IsA('ObjectValue') and v.Name == 'Tool' and v.Value == Child then
RemoveFromBB(v.Parent)
ToggleBackpack() -- Displays BetterBackpack or not based on if there's anything to display
end
end
end
end
end)
Player.Character.ChildRemoved:Connect(function(Child)
RunService.Heartbeat:Wait() --Lets give it a moment to return to backpack
if Child.Parent ~= Backpack then
for i,v in pairs(script.Parent:GetDescendants()) do -- Uh oh, tool wasn't returned to backpack. Lets get rid of any slots with this tool
if v:IsA('ObjectValue') and v.Name == 'Tool' and v.Value == Child then
ToggleHighlight(v.Parent, false)
RemoveFromBB(v.Parent)
ToggleBackpack() -- Displays BetterBackpack or not based on if there's anything to display
end
end
CurrentlySelected = nil
end
end)
Player.Character.ChildAdded:Connect(function(Child)
if Child:IsA('Tool') then
if not CheckForTool(Child) then -- Lets not re-add the same tool twice
AddToBB(Child)
ToggleBackpack() -- Displays BetterBackpack or not based on if there's anything to display
end
for i,v in pairs(script.Parent:GetDescendants()) do
if v:IsA('ObjectValue') and v.Name == 'Tool' then
if v.Value == Child then
ToggleHighlight(v.Parent, true)
CurrentlySelected = v.Parent
else
ToggleHighlight(v.Parent, false)
end
end
end
end
end)
----------[[ HOTBAR KEYBINDS ]]----------
local Keys = {
[Enum.KeyCode.One] = 1,
[Enum.KeyCode.Two] = 2,
[Enum.KeyCode.Three] = 3,
[Enum.KeyCode.Four] = 4,
[Enum.KeyCode.Five] = 5,
[Enum.KeyCode.Six] = 6,
[Enum.KeyCode.Seven] = 7,
[Enum.KeyCode.Eight] = 8,
[Enum.KeyCode.Nine] = 9,
[Enum.KeyCode.Zero] = 0,
}
UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
if not GameProcessed and Keys[InputObject.KeyCode] then
local Num = Keys[InputObject.KeyCode]
local Slot
for i,v in pairs(Hotbar:GetChildren()) do
if v:IsA('Frame') and tostring(Num) == v.TextLabel.Text then
Slot = v
end
end
if Slot and Slot.Tool.Value ~= nil then
ToggleEquip(Slot)
end
end
end)
----------[[ SETTINGS ]]----------
BBSettings.AttributeChanged:Connect(function(Attribute)
Settings[Attribute] = BBSettings:GetAttribute(Attribute) -- Updates settings table whenever an attribute is changed
if Attribute == 'InventoryEnabled' then
if Settings['BackpackEnabled'] == true then
ToggleInventory(Settings['InventoryEnabled'])
end
elseif Attribute == 'BackpackEnabled' then
if Settings['BackpackEnabled'] == true then
script.Parent.Enabled = true
ToggleInventory(Settings['InventoryEnabled'])
else
script.Parent.Enabled = false
ToggleInventory(false)
end
end
end)
----------[[ DISABLE CORE BACKPACK ]]----------
game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
-- when player moves something in hotbar, save tool name and position
-- when player respawns, look for saved tools and place them. fill remaining spots with any other tools
-- if player gets a tool that was saved, add it to position and displace any existing tool to the next available slot (or inventory if no slots available)