robloxapp-20240907-1858304.wmv (2.4 MB) [Very sorry for the low quality since I can’t use OBS]
Open to feedback as to how I can improve this. I have 9 slots for each item frame that can be equipped which are preset into a toolbar frame. I use Q to drop the item and the numbers on the frame as the keybinds to equip and unequip
If you want to read my code here it is (its long)
Code
Module:
local toolbarmodule = {}
toolbarmodule.__index = toolbarmodule
toolbarmodule.Keybinds = {
['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
}
function toolbarmodule:new(player)
local self = setmetatable(toolbarmodule,{})
self.slots = script.Parent.Parent.ToolbarFrame.Slots
self.itemframes = self.slots.Parent.ItemFrames
self.player = player
return self
end
function toolbarmodule:GetFrameFromKeybind(keybind)
for i,v in pairs(self.itemframes:GetChildren()) do
if v:GetAttribute('Slot') == keybind then return v end
end
end
function toolbarmodule:Equip(keybind : number)
local frame = self:GetFrameFromKeybind(keybind)
local fslot = self.slots:FindFirstChild('Slot'..keybind)
if not frame or not fslot then return end
if frame:GetAttribute('IsEquipped') then self:Unequip(frame,fslot) return end
for i,v in pairs(self.itemframes:GetChildren()) do if v:GetAttribute('IsEquipped') then self:Unequip(v,fslot) end end
frame:SetAttribute('IsEquipped',true)
frame.EquipStatus.Text = 'Equipped';frame.EquipStatus.TextColor3 = Color3.fromRGB(0,255,0)
self.player.Backpack:FindFirstChild(frame.Name).Parent = self.player.Character
end
function toolbarmodule:Unequip(frame,slot)
frame:SetAttribute('IsEquipped',false)
frame.EquipStatus.Text = 'Unequipped';frame.EquipStatus.TextColor3 = Color3.fromRGB(255,0,0)
self.player.Character:FindFirstChild(frame.Name).Parent = self.player.Backpack
end
function toolbarmodule:Addtoframe(an,is,io,ritem)
if is == Enum.UserInputState.Begin then
if not self.itemframes:FindFirstChild(ritem.Name) and self.itemframes:GetAttribute('Items') < 9 then
local frame = script.Parent.Parent.Parent.Template:Clone()
self.itemframes:SetAttribute('Items',self.itemframes:GetAttribute('Items') + 1)
frame:SetAttribute('Slot',self.itemframes:GetAttribute('Items'))
local fslot = self.slots:FindFirstChild('Slot'..frame:GetAttribute('Slot'))
frame.Position = fslot.Position
frame.Size = fslot.Size
frame.Name = ritem.Name
frame.KeybindLabel.Text = frame:GetAttribute('Slot')
frame.Parent = self.itemframes
ritem.Parent = self.player.Backpack
local item = ritem:Clone()
item.Parent = frame
local cam = Instance.new('Camera',frame)
cam.CFrame = CFrame.new(item.Handle.Position + (item.Handle.CFrame.LookVector * 3),item.Handle.Position)
frame.CurrentCamera = cam
end
end
end
function toolbarmodule:Remove(frame)
if not frame:GetAttribute('IsEquipped') then return end
self.itemframes:SetAttribute('Items',self.itemframes:GetAttribute('Items') - 1)
local tool = self.player.Character:FindFirstChild(frame.Name)
tool.Handle.CanCollide = true
tool.Parent = self.player.Backpack
wait()
tool.Parent = workspace.Pickables
tool.Handle.CFrame = self.player.Character.HumanoidRootPart.CFrame + (self.player.Character.HumanoidRootPart.CFrame.LookVector * 5)
frame:Destroy()
for i,v in pairs(self.itemframes:GetChildren()) do
if v:GetAttribute('Slot') < frame:GetAttribute('Slot') then continue end
v:SetAttribute('Slot',v:GetAttribute('Slot') - 1)
local vslot = self.slots:FindFirstChild('Slot'..v:GetAttribute('Slot'))
v.Position = vslot.Position
v.KeybindLabel.Text = v:GetAttribute('Slot')
end
end
return toolbarmodule
Local script:
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
local cas = game:GetService('ContextActionService')
local uis = game:GetService('UserInputService')
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tms = require(script.ToolbarModule)
local toolobj = tms:new(player)
uis.InputChanged:Connect(function(i,p)
if p then return end
if mouse.Target and mouse.Target:FindFirstAncestor('Pickables') then
local item = mouse.Target.Parent
cas:BindAction(
"PickupTool",
function(an,is,io)
toolobj:Addtoframe(an,is,io,item)
end,
true,
Enum.KeyCode.E
)
else
cas:UnbindAction('PickupTool')
end
end)
player.Character.ChildAdded:Connect(function(child)
if not script.Parent.ToolbarFrame.ItemFrames:FindFirstChild(child.Name) and child:IsA('Tool') then
tms:Addtoframe(nil,Enum.UserInputState.Begin,nil,child)
end
end)
uis.InputBegan:Connect(function(i,p)
local num = tms.Keybinds[tostring(i.KeyCode)]
if num then toolobj:Equip(num) end
if i.KeyCode == Enum.KeyCode.Q then
for i,v in pairs(script.Parent.ToolbarFrame.ItemFrames:GetChildren()) do
if v:GetAttribute('IsEquipped') then toolobj:Remove(v) break end
end
end
end)