Toolbar Help / UserInputService

Problem 1

I am working on a custom toolbar, but the guns I made locks the mouse so the player cannot click the buttons. So I started working on making keybinds (1,2,3,4,5) and it isn’t working. There are no errors, so I don’t know why it won’t work.

Everything else works just not the keybind part.

local items = {}
local frames = {}
local equipped = nil
local player = game.Players.LocalPlayer
local character = player.Character
local uis = game:GetService("UserInputService")

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

local function Scan(location)
	for i,v in pairs(location:GetChildren()) do
		if v:IsA("Tool") then
			table.insert(items, v)
		end
	end
end

local function Update()
	for i,v in pairs(frames) do
		v:Destroy()
	end
	for i,v in pairs(items) do
		local sample = script.Sample:Clone()
		sample.Name = v.Name
		sample.Parent = script.Parent.Holder
		sample.Image = v.TextureId
		table.insert(frames, sample)
		sample.MouseButton1Click:Connect(function()
			if equipped == nil or equipped ~= v then
				equipped = v
				character.Humanoid:UnequipTools()
				wait()
				character.Humanoid:EquipTool(v)
			else
				equipped = nil
				character.Humanoid:UnequipTools()
			end
		end)
		
		if i ==  1 then
			KeyCode = Enum.KeyCode.One
			print(i)
		elseif i == 2 then
			KeyCode = Enum.KeyCode.Two
			print(i)
		elseif i == 3 then
			KeyCode = Enum.KeyCode.Three
		elseif i == 4 then
			KeyCode = Enum.KeyCode.Four
		elseif i == 5 then
			KeyCode = Enum.KeyCode.Five
		elseif i == 6 then
			KeyCode = Enum.KeyCode.Six
		end
		
		uis.InputBegan:Connect(function(input, event)
			if input == KeyCode then
				print("input")
				if equipped == nil or equipped ~= v then
					print("equip")
					equipped = v
					character.Humanoid:UnequipTools()
					wait()
					character.Humanoid:EquipTool(v)
				else
					equipped = nil
					character.Humanoid:UnequipTools()
				end
			end
		end)

	end
end

local function BackpackChanged()
	items = {}
	Scan(character)
	Scan(player.Backpack)
	Update()
end

BackpackChanged()

player.Backpack.ChildAdded:Connect(BackpackChanged)
player.Backpack.ChildRemoved:Connect(BackpackChanged)

character.ChildAdded:Connect(BackpackChanged)
character.ChildRemoved:Connect(BackpackChanged)

Problem 2

Would roblox automatically limit the amount of tools you can have? Or do I do this manually, if so how?

1 Like

Just a heads up, I only need help on the keybind part, if you have seen a different topic like this could you link it?

Maybe this would work:

uis.InputBegan:Connect(function(input, event)
    if input.KeyCode == KeyCode then
1 Like

I am testing it but the script just loops throught and changes the part to 1, 2 etc. Hard to explain but it is really laggy

I believe this is the problem right here, you are going through a table of the items and you are just changing the KeyCode to Enum.KeyCode.Six every time because it ends on the index of six and cancels out the rest of the i’s in the pairs loop.

1 Like

How should I do this then? I can’t think of anything else atm

if i ==  1 then
			KeyCode = Enum.KeyCode.One
			print(i)
		elseif i == 2 then
			KeyCode = Enum.KeyCode.Two
			print(i)
		elseif i == 3 then
			KeyCode = Enum.KeyCode.Three
		elseif i == 4 then
			KeyCode = Enum.KeyCode.Four
		elseif i == 5 then
			KeyCode = Enum.KeyCode.Five
		elseif i == 6 then
			KeyCode = Enum.KeyCode.Six
		end

What is the purpose of this?

1 Like

Is this part of checking for a relevant input?

1 Like

I am using it because I cannot simple do Enum.KeyCode.i

If you know of a better way then could you share?

So this is meant to check if the keycode is either 1,2,3 and so on?

1 Like

Well, there are multiple ways of doing this.

For example, you could simply make an array of keycodes and check if the input keycode is in the table.

local KeyList = {
	Enum.KeyCode.A;
	Enum.KeyCode.B;
	Enum.KeyCode.C;
}

game:GetService("UserInputService").InputBegan:Connect(function(input,gpe)
	if not gpe and table.find(KeyList, input.KeyCode) then
		print("Relevant input!")
	end
end)

Alternatively, you could create a function that will return true if the input is relevant:

local function RelevantInput(input)
	
	return (
		input == Enum.KeyCode.A or
		input == Enum.KeyCode.B or
		input == Enum.KeyCode.C or
		input == Enum.KeyCode.D
	);
	
end

game:GetService("UserInputService").InputBegan:Connect(function(input,gpe)
	if not gpe and RelevantInput(input.KeyCode) then
		print("Relevant input!")
	end
end)

I have not tested this code, but it should work.

1 Like

Does my existing way also work, or should try the ways you suggested?

Also I want to make sure it chooses the correct item in the list Enum.KeyCode.One = Item 1

One problem is that the list layout is by LayoutOrder and not name, so whenever it is equipped it moves to the front which can be confusing, but if I do it by name, what appears to be the first item actually would be the 2nd and detecting the input of 2. if that makes any sense.

You could maybe make the “KeyList” array a dictionary that holds a value which can refer to the item.

local KeyList = {
	[Enum.KeyCode.One] = "Item1";
	[Enum.KeyCode.Two] = "Item2";
	[Enum.KeyCode.Three] = "Item3";
}

game:GetService("UserInputService").InputBegan:Connect(function(input,gpe)
	if not gpe and KeyList[input.KeyCode] then
           local Item = KeyList[input.KeyCode]
		print("Relevant input! The item's name is: "..Item)
	end
end)
1 Like

Also UIListLayout’s LayoutOrder property can be changed to sort by name.

1 Like

I’ll try that. 30 characters

I can go into more depth but I think this would be true

Hold on, I’ve made a little error in the code. Let me fix that.

1 Like

What you can then do is pass the item name as an argument like so:

function EquipTool(item)
local item = workspace:FindFirstChild(item) -- Wherever the item is stored
Humanoid:EquipTool(item)
end
1 Like