What do you want to achieve?
I am currently making an L4D-like game where you can pick up primary guns and secondary guns.
I have set up tooltips on each gun to tell which is a secondary and which is a primary.
What is the issue?
I want to make it so primary guns are locked to key-bind 1 and secondary guns are locked to key-bind 2.
What solutions have you tried so far?
I’ve tried searching up my problem to no avail. Any help would be GREATLY appreciated.
(The script I made is basically just a recreation of the backpack without the GUI.)
Script:
local maxOnHotbar = 5
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local UserInputService = game:GetService("UserInputService")
local p = Players.LocalPlayer
local Backpack = p.Backpack
local Character = p.Character
local KEYS = {One = 1, Two = 2, Three = 3, Four = 4, Five = 5}
local items = {}
local function handleEquip(tool)
if tool.Parent ~= Character then
Character.Humanoid:EquipTool(tool)
end
end
local function updateAdd(tool)
if not tool:IsA("Tool") then
return
end
local toolname = tool.Name
if table.find(items, tool) then
return
end
table.insert(items, tool)
end
local function updateRemove(tool)
if not tool:IsA("Tool") then
return
end
if tool.Parent == Character or tool.Parent == Backpack then
return
end
if table.find(items, tool) then
local index = table.find(items, tool)
table.remove(items, index)
end
end
while true do
local success, err = pcall(function()
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
end)
if success then
break
end
wait()
end
for _, tool in pairs(Backpack:GetChildren()) do
updateAdd(tool)
end
Backpack.ChildAdded:Connect(updateAdd)
Backpack.ChildRemoved:Connect(updateRemove)
Character.ChildAdded:Connect(updateAdd)
Character.ChildRemoved:Connect(updateRemove)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then
return
end
if KEYS[input.KeyCode.Name] then
local index = KEYS[input.KeyCode.Name]
if items[index] then
handleEquip(items[index])
end
end
end)
okay you just need to check what weapon is equipped once you pick-up another weapon it removes the selected weapon, (to do this simply get the selected items index in the items table) then remove it and replace it with the new weapon/item and pass the index you got from finding the current equipment weapon. it as simple as passing a index and replace the old weapon with the new but when adding it to the items table pass the index you got from the weapon that was equipped
local maxOnHotbar = 5
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local UserInputService = game:GetService("UserInputService")
local p = Players.LocalPlayer
local Backpack = p.Backpack
local Character = p.Character
local KEYS = {One = 1, Two = 2, Three = 3, Four = 4, Five = 5}
local items = {}
local CurrentEquipt
local function handleEquip(tool)
if tool.Parent ~= Character then
Character.Humanoid:EquipTool(tool)
CurrentEquipt = tool
end
end
local function updateAdd(tool: Tool)
if not tool:IsA("Tool") then
return
end
local toolname = tool.Name
local tooltype = tool.ToolTip
if table.find(items, tool) then
return
end
local CurrentToolIndex = table.find(items,CurrentEquipt)
table.remove(items,CurrentToolIndex)
table.insert(items,tool,CurrentToolIndex)
end
local function updateRemove(tool)
if not tool:IsA("Tool") then
return
end
if tool.Parent == Character or tool.Parent == Backpack then
return
end
if table.find(items, tool) then
local index = table.find(items, tool)
table.remove(items, index)
end
end
while true do
local success, err = pcall(function()
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
end)
if success then
break
end
task.wait(1)
end
for _, tool in pairs(Backpack:GetChildren()) do
updateAdd(tool)
end
Backpack.ChildAdded:Connect(updateAdd)
Backpack.ChildRemoved:Connect(updateRemove)
Character.ChildAdded:Connect(updateAdd)
Character.ChildRemoved:Connect(updateRemove)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then
return
end
if KEYS[input.KeyCode.Name] then
local index = KEYS[input.KeyCode.Name]
if items[index] then
handleEquip(items[index])
end
end
end)