Help for binding keys 1, 2, 3 for custom Backpack Gui

Hey, I’m Lap_124, a Roblox scripter.

You will probably taking me for a noob :upside_down_face:

I made a working backpack Gui but I didn’t manage to bind keys 1, 2, 3 to equip tools because I disabled default backpack Gui.

Please help me I maybe passed around 1 hour into it.

2 Likes

I think it’s because i put script into starterplayerscript

What have you tried so far? It’d be helpful to have extra information.

You can use UserInputService to register keyboard inputs. Please note for mobile devices you may need buttons to navigate your backpack UI.

I’ve provided an example below on how to use the service.

game:GetService("UserInputService").InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.One then --Your input
		--Do code
	end
end)

I tried UIS 50Alpha…
Where i need to insert the script ?

here’s what I tried sorry if it’s a little messy
local UIS = game:GetService(“UserInputService”)

-- Player Variables
local Player = game.Players.LocalPlayer
local Backpack = Player:WaitForChild("Backpack") or Player.Backpack:Wait()
local char = Player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

UIS.InputBegan:Connect(function(input)
	local num = 0
	if input.KeyCode == Enum.KeyCode.One then
		num = 1
	elseif input.KeyCode == Enum.KeyCode.Two then
		num = 2
	elseif input.KeyCode == Enum.KeyCode.Three then
		num = 3
	end
	if num ~= 0 then
		print(num)
		local Items = Backpack:GetChildren()
		local toolToEquip = Items[num]

		if toolToEquip then
			for i, v in pairs(Items) do
				if v:IsA("Tool") then
					print(v.Name.." is on ur backpack")
					v.Equipped:Connect(function()
						hum:UnequipTools(v)
					end)
				end
			end
			hum:EquipTool(toolToEquip)

		end
	end
end)
1 Like

Does it print any errors to the log, or is the log empty?

1 Like

It doesn’t print any error can you correct it and tell me where place script

There is errors with your script but but the main issue is that if your tools are in different orders when equipping tools with be inconsistent. For example 1 may equip “a” tool but then you unequip the tool it will be placed somewhere else in the backpack directory.

What can I do so ?
I think I should rewrite entirely my script

The easiest way in my opinion would to be add values to tools and look for them when you equip a tool. For example Tool A will have a value of 1 so when you click 1 it find that exact tool and Tool B will have a value of 2 so when you click 2 it will also find that exact tool, and so on.

1 Like

I’ll try to rewrite my code.
Question: Do I need to place script in starterplayerscipt or somewhere else?

I recommend placing the script within your UI instance.

I noticed that if I place the script inside StarterGui, UIS and CAS(ContextActionService) won’t work

1 Like

What’s the type of the script, after all?

LocalScript
but here i’m trying to use a remote event for the system

You must place the script within a ScreenGui Instance.

Are you sure ? Last time I tried UIS and CAS didnt work here

Wait… I remaked my system with a remote event and it seems that it works !

2 Likes

Wait could u explain how u figured it out? I’m making a cod zombie game and i managed to figure out the mystery box, perks, pack-a-punch, power and much more…but I couldn’t figure out this lmao. So can u explain the whole remote event thing tysm!