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?
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.
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
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)
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)