Specific tools going to specific slots

(First post on here go easy please.)

  1. 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.

  2. 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.

  3. What solutions have you tried so far?
    I’ve tried searching up my problem to no avail. Any help would be GREATLY appreciated.

Video of my problem:
robloxapp-20220222-2256081.wmv (3.3 MB)

(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)
1 Like

I understand what you are saying but can you go into depth of what you are trying to make it do

I am trying to make it so that primary weapons are in slot 1 (so pressing 1 would equip it) and the same goes for all of the binds.

I forgot to mention I am also trying to make it so when you have another primary in your inventory, it goes into slot 1 instead of slot 2.

How do I know what weapons are PrimaryWeapons and what Weapons Are Secondary ?

In the tool properties, there is a string named “ToolTip”. I have basically used that part to tell if a weapon is a primary or secondary.

also you want to be able to switch between Primary weapons with 1 keybind?

I want it when you get another primary when you already have one, the second primary replaces the first one.

okay the only way i feel i can understand this since english is a 4th language to me is a call got discord and time to descuss this?

Stylex#6742

I don’t think I have time for a discord call, basically, I just want one primary and one secondary for the player at all times.

okay and it replaces what ever weapon is currently selected?

Yes, I’m pretty sure that’s what I want.

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

also sorry for dragging just had trouble understanding the end goal.

Sorry for dragging this on for too long and I thank you for sticking around but could I have a code example of how to do this?

sure 1 moment let me make a example

also would you mind me getting a example code of how its added to the player? the code

Like where it’s located? It’s located in StarterGUI and is a LocalScript.

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)

let me know what errors it gives please