Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --PASTE INTO LOCAL SCRIPT!
- --LOCAL SCRIPT GOES INTO STARTER PLAYER SRIPTS!
- --press I to open the inventory
- --right click on a item to remove it from the hotbar
- --left click a hotbar slot to toggle the item equip
- --[[left click a item in the inventory to select it
- then left click the hotbar slot you want to put it
- in]]
- --[[If the tool you pick up dosnt have a texture ID
- the image will be set to black and yellow lines.]]
- -- Removes default backpack
- game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
- --services
- local players = game:GetService("Players")
- local UIS = game:GetService("UserInputService")
- --var
- local player = players.LocalPlayer
- local character = player.Character or player.CharacterAdded:Wait()
- local humanoid = character:WaitForChild("Humanoid")
- local mouse = player:GetMouse()
- local screenGui = Instance.new("ScreenGui", player.PlayerGui)
- local backpack = {}
- local debounce = false
- local disable = false
- local backspace = false
- local selectedItem;
- local hotbar = {}
- local scrollingFrame = Instance.new("ScrollingFrame", screenGui)
- --creates a list array to store hotbar data
- local function HotbarArray()
- --[[creates nil data so that it can
- later be replaced with item data
- and maintaine its length]]
- local hotbarSize = 10
- for i = 1, hotbarSize do
- if table.getn(hotbar) == 0 then
- hotbar = {nil}
- else
- table.insert(hotbar, nil)
- end
- end
- end
- HotbarArray()
- local function CreateInventory()
- --sets the layout type
- local gridLayout = Instance.new("UIGridLayout", scrollingFrame)
- --property's
- scrollingFrame.Size = UDim2.new(.792,0,.791,0)
- scrollingFrame.Position = UDim2.new(.89,0,.85,0)
- scrollingFrame.Visible = false
- scrollingFrame.BackgroundColor3 = Color3.new(0, 0, 1)
- scrollingFrame.BorderColor3 = Color3.new(0,0,0)
- gridLayout.CellSize = UDim2.new(0,60,0,60)
- scrollingFrame.AnchorPoint=Vector2.new(1,1)
- end
- CreateInventory()
- local function EquipTool(item)
- --debounce and wait is added to prevent bugs
- debounce = true
- humanoid:EquipTool(item)
- wait()
- debounce = false
- end
- local function ToggleTool(index)
- debounce = true
- --[[checks to see if the item is already in use
- to determine rather to unequip or equip]]
- local itemInUse = false
- --searches the character for the clicked item
- for _, v in ipairs(character:GetChildren()) do
- if v.ClassName == "Tool" and v == hotbar[index] then
- humanoid:UnequipTools()
- itemInUse = true
- end
- end
- --Equips the tool
- if not itemInUse then
- humanoid:EquipTool(hotbar[index])
- end
- debounce = false
- end
- local function RemoveFromHotbar(slot, index)
- --clears hotbar slot
- slot.Full.Value = nil
- slot.Image = ""
- table.remove(hotbar, index)
- table.insert(hotbar, index, nil)
- end
- local function RemoveFromBackpack(slot, item)
- table.remove(backpack, table.find(backpack, item))
- slot:Destroy()
- end
- local function AddToBackpack(itemAdded)
- --adds item to list array
- if table.getn(backpack) == 0 then
- --sets list value
- backpack = {itemAdded}
- else
- --adds item to list
- table.insert(backpack, itemAdded)
- end
- --[[creates a slot for that item to be
- visible in the inventory]]
- local slot = Instance.new("ImageButton", scrollingFrame)
- slot.Name = "InventorySlot"
- --if the items texture id is blank a default image is set
- if itemAdded.TextureId == "" then
- slot.Image = "http://www.roblox.com/asset/?id=12249128399"
- else
- slot.Image = itemAdded.TextureId
- end
- slot.BorderSizePixel = 3
- slot.BorderColor = BrickColor.new("Black")
- slot.Size = UDim2.new(0,60,0,60)
- local val = Instance.new("ObjectValue", slot)
- val.Name = "Full"
- val.Value = itemAdded
- --selects item in that slot that was clicked
- slot.MouseButton1Click:Connect(function()
- slot.BorderColor = BrickColor.new("Really red")
- selectedItem = val.Value
- end)
- --unselects item in the slot
- local function UnselectItem(Slot)
- Slot.BorderColor = BrickColor.new("Black")
- selectedItem = nil
- end
- --triggeres unselect
- slot.MouseButton2Down:Connect(function()
- UnselectItem(slot)
- end)
- --uhselects if inventory is closed
- scrollingFrame.Changed:Connect(function()
- if scrollingFrame.Visible == false then
- UnselectItem(slot)
- end
- end)
- end
- local function RefreshHotbar()
- --local var
- local index = 1
- --scans through hotbar
- for _, slot in ipairs(screenGui.Hotbar:GetChildren()) do
- if slot.Name == "Slot" then
- --if there is no item for that hotbar slot it gets cleared and the for loop ends
- if hotbar[index] == "void" or hotbar[index] == nil then
- --clears hotbar slot
- slot.Full.Value = nil
- slot.Image = ""
- table.remove(hotbar, index)
- table.insert(hotbar, index, nil)
- index += 1
- else
- --if there is an item then the slot updates
- local item = hotbar[index]
- slot.Full.Value = item
- --if the items texture id is blank a default image is set
- if item.TextureId == "" then
- slot.Image = "http://www.roblox.com/asset/?id=12249128399"
- else
- slot.Image = item.TextureId
- end
- index += 1
- end
- if slot.Full.Value ~= nil then
- local bool, errorMessage = pcall (function()
- if not character:FindFirstChild(slot.Full.Value.Name) and not player.Backpack:FindFirstChild(slot.Full.Value.Name) then
- --clears hotbar slot
- slot.Full.Value = nil
- slot.Image = ""
- table.remove(hotbar, index)
- table.insert(hotbar, index, nil)
- index += 1
- end
- local tool = slot.Full.Value
- if table.getn(tool:GetChildren()) == 0 then
- --clears hotbar slot
- slot.Full.Value = nil
- slot.Image = ""
- table.remove(hotbar, index)
- table.insert(hotbar, index, nil)
- index += 1
- end
- end)
- end
- end
- end
- end
- local function AddToHotbar(slot, item)
- local index = slot.slotIndex.Value
- hotbar[index] = item
- RefreshHotbar()
- end
- local function CreateHotbar()
- --creates hotbar frame
- local HotbarGui = Instance.new("Frame", screenGui)
- HotbarGui.Transparency = 1
- HotbarGui.Name = "Hotbar"
- HotbarGui.Size = UDim2.new(0.673, 0,0.098, 0)
- HotbarGui.Position = UDim2.new(.95, 0,1, 0)
- HotbarGui.AnchorPoint = Vector2.new(1,1)
- --adds grid layout for slots
- local gridLayout = Instance.new("UIGridLayout", HotbarGui)
- gridLayout.CellSize = UDim2.new(0,60,0,60)
- --creates 10 hotbar slots
- for i = 1, 10 do
- --creates slot
- local slot = Instance.new("ImageButton", HotbarGui)
- slot.Name = "Slot"
- slot.BorderSizePixel = 3
- slot.BorderColor = BrickColor.new("Black")
- --creates object value to keep track items in each slot
- local val = Instance.new("ObjectValue", slot)
- val.Name = "Full"
- --creates a frame for the textlabel
- local frame = Instance.new("Frame", slot)
- frame.Size = UDim2.new(.3, 0, .3, 0)
- frame.Position = UDim2.new(.7, 0, 0, 0)
- Instance.new("UICorner", frame)
- --text label displayes slots keybind number
- local Text = Instance.new("TextLabel", frame)
- --sets the text to i enless its the 10th slot then its set to 0
- if i < 10 then
- Text.Text = i
- elseif i == 10 then
- Text.Text = 0
- end
- --sets text properties
- Text.BackgroundTransparency = 1
- Text.TextScaled = true
- Text.Size = UDim2.new(1,0,1,0)
- --keeps track of the horbar slots index
- local slotIndex = Instance.new("IntValue", slot)
- slotIndex.Name = "slotIndex"
- slotIndex.Value = i
- slot.MouseButton1Click:Connect(function()
- --if an item is selected it will move it to the slot
- if selectedItem ~= nil then
- --if the slot it wants is full it will move the
- --item in that slot to the inventory
- if slot.Full.Value ~= nil then
- local item = slot.Full.Value
- AddToBackpack(item)
- end
- RemoveFromHotbar(slot, slotIndex.Value)
- --adds item and unselects it
- AddToHotbar(slot, selectedItem)
- --locates and deletes the slot
- for _, Slot in ipairs(screenGui.ScrollingFrame:GetChildren()) do
- if Slot.Name == "InventorySlot" then
- if Slot.Full.Value == selectedItem then
- RemoveFromBackpack(Slot, selectedItem)
- end
- end
- end
- selectedItem = nil
- else
- --else it will just select the item in that slot
- ToggleTool(slotIndex.Value)
- end
- end)
- --Puts tool away in inventory
- slot.MouseButton2Click:Connect(function()
- if slot.Full.Value ~= nil then
- local item = val.Value
- humanoid:UnequipTools()
- AddToBackpack(item)
- RemoveFromHotbar(slot, slotIndex.Value)
- end
- end)
- end
- end
- CreateHotbar()
- --Keybinds
- UIS.InputBegan:Connect(function(input)
- local key = input.KeyCode
- --inventory keybind
- if key==Enum.KeyCode.I then
- local inv = screenGui.ScrollingFrame
- --toggles visibility
- if inv.Visible == true then inv.Visible = false else inv.Visible = true end
- end
- --
- if key == Enum.KeyCode.Backspace then
- backspace = true
- end
- --hotbar keybinds
- if key==Enum.KeyCode.One then
- if hotbar[1] then
- ToggleTool(1)
- end
- end
- if key==Enum.KeyCode.Two then
- if hotbar[2] then
- ToggleTool(2)
- end
- end
- if key==Enum.KeyCode.Three then
- if hotbar[3] then
- ToggleTool(3)
- end
- end
- if key==Enum.KeyCode.Four then
- if hotbar[4] then
- ToggleTool(4)
- end
- end
- if key==Enum.KeyCode.Five then
- if hotbar[5] then
- ToggleTool(5)
- end
- end
- if key==Enum.KeyCode.Six then
- if hotbar[6] then
- ToggleTool(6)
- end
- end
- if key==Enum.KeyCode.Seven then
- if hotbar[7] then
- ToggleTool(7)
- end
- end
- if key==Enum.KeyCode.Eight then
- if hotbar[8] then
- ToggleTool(8)
- end
- end
- if key==Enum.KeyCode.Nine then
- if hotbar[9] then
- ToggleTool(9)
- end
- end
- if key==Enum.KeyCode.Zero then
- if hotbar[10] then
- ToggleTool(10)
- end
- end
- end)
- UIS.InputEnded:Connect(function(input)
- local key = input.KeyCode
- if key == Enum.KeyCode.Backspace then
- backspace = false
- print(backspace)
- end
- end)
- --Attempts to add item to backpack
- player.Backpack.ChildAdded:Connect(function(itemAdded)
- if table.getn(hotbar) >= 10 and debounce == false then
- if not table.find(hotbar, itemAdded) and not table.find(backpack, itemAdded) then
- AddToBackpack(itemAdded)
- end
- end
- end)
- --picks up an item
- character.ChildAdded:Connect(function(itemAdded)
- --checks if the player dosnt already own the item
- if not table.find(backpack, itemAdded.Name) or not table.find(hotbar, itemAdded.Name) then
- --searches through the hotbar
- for _, slot in ipairs(screenGui.Hotbar:GetChildren()) do
- if slot.Name == "Slot" then
- --checks conditions to prevent an error
- if itemAdded.ClassName == "Tool" and debounce == false then
- --checks if hotbar is full
- if table.getn(hotbar) < 10 and debounce == false then
- if slot.Full.Value == nil then
- AddToHotbar(slot, itemAdded)
- break
- end
- --if hotbar is full adds to inventory
- elseif debounce == false then
- AddToBackpack(itemAdded)
- break
- end
- end
- end
- end
- end
- end)
- --adds starter gear
- for _, v in ipairs(player.Backpack:GetChildren()) do
- --searches through the hotbar
- for _, slot in ipairs(screenGui.Hotbar:GetChildren()) do
- if slot.Name == "Slot" then
- --checks conditions to prevent an error
- if v.ClassName == "Tool" and debounce == false then
- --checks if hotbar is full
- if table.getn(hotbar) < 10 and debounce == false then
- if slot.Full.Value == nil then
- AddToHotbar(slot, v)
- break
- end
- --if hotbar is full adds to inventory
- elseif debounce == false then
- AddToBackpack(v)
- break
- end
- end
- end
- end
- end
- --drops item
- character.ChildRemoved:Connect(function(itemRemoved)
- --makes sure item wasnt just moved to backpack
- if not table.find(backpack, itemRemoved) then
- local index = table.find(hotbar, itemRemoved)
- --searches through hotbar for the right slot
- for _, slot in ipairs(screenGui.Hotbar:GetChildren()) do
- if slot.Name == "Slot" then
- task.wait()
- --checks the slots index and if backspace is pressed
- if slot.slotIndex.Value == index and backspace == true then
- RemoveFromHotbar(slot, index)
- end
- end
- end
- end
- end)
- while true do
- task.wait()
- RefreshHotbar()
- end
Add Comment
Please, Sign In to add comment