How would I make my custom hotbar have numbered keybinds?

So, I have my custom hotbar (I know it looks like garbage, I’m not working on the looks quite yet) and I want it so when I press 1, it equips the first item, and when I press 2, the second item, etc.

Here is the hotbar (Look great, right?):

I have tried this in the past but I have not succeeded. I really need this as in the game I’m making, you are forced into shiftlock.

I’ve tried to search for solutions, but I haven’t found any and I really need help here.

Right now this is my script (it probably sucks but I just wanna have numbered keybinds-)

local Players = game:GetService("Players")
local inputService = game:GetService("UserInputService")
local repStorage = game:GetService("ReplicatedStorage")
local soundService = game:GetService("SoundService")

local events = repStorage:WaitForChild("Events")
local items = repStorage:WaitForChild("Items")
local particles = repStorage:WaitForChild("Particles")
local sounds = soundService:WaitForChild("Sounds")

local itemTemplates = workspace.ItemTemplates

local itemInfo = {
	["Bloxy Cola"] = {
		["Image"] = "rbxassetid://3361929763",
		["Rarities"] = {
			["Common"] = {
				["weight"] = 1
			},
			["Uncommon"] = {
				["weight"] = 0.75
			},
			["Rare"] = {
				["weight"] = 0.5
			},
			["Epic"] = {
				["weight"] = 0.25
			},
			["Legendary"] = {
				["weight"] = 0.1
			},
		},
		["Animation"] = "rbxassetid://85667289721351"
		--["Rarity"] = "Common"
	},
	["Witchs Brew"] = {
		["Image"] = "rbxassetid://1435251778",
		["Rarities"] = {
			["Common"] = {
				["weight"] = 0.8
			},
			["Uncommon"] = {
				["weight"] = 0.35
			},
			["Rare"] = {
				["weight"] = 0.6
			},
			["Epic"] = {
				["weight"] = 0.25
			},
			["Legendary"] = {
				["weight"] = 0.1
			},
		},
		["Animation"] = "rbxassetid://85667289721351"
		--["Rarity"] = "Common"
	},
	["Goala Cola"] = {
		["Image"] = "rbxassetid://18653829587",
		["Rarities"] = {
			["Common"] = {
				["weight"] = 0.5
			},
			["Uncommon"] = {
				["weight"] = 1
			},
			["Rare"] = {
				["weight"] = 0.4
			},
			["Epic"] = {
				["weight"] = 0.35
			},
			["Legendary"] = {
				["weight"] = 0.15
			},
		},
		["Animation"] = "rbxassetid://85667289721351"
		--["Rarity"] = "Common"
	},
	["Linked Sword"] = {
		["Image"] = "rbxassetid://534533607",
		["Rarities"] = {
			["Common"] = {
				["weight"] = 0.4
			},
			["Uncommon"] = {
				["weight"] = 1.1
			},
			["Rare"] = {
				["weight"] = 0.4
			},
			["Epic"] = {
				["weight"] = 0.3
			},
			["Legendary"] = {
				["weight"] = 0.05
			},
		},
		["Animation"] = "rbxassetid://73571466942836"
		--["Rarity"] = "Common"
	},
	["Firebrand"] = {
		["Image"] = "rbxassetid://137293157979040",
		["Rarities"] = {
			["Common"] = {
				["weight"] = 0.1
			},
			["Uncommon"] = {
				["weight"] = 0.3
			},
			["Rare"] = {
				["weight"] = 0.4
			},
			["Epic"] = {
				["weight"] = 1
			},
			["Legendary"] = {
				["weight"] = 0.15
			},
		},
		--["Rarity"] = "Common"
		["Animation"] = "rbxassetid://73571466942836"
	},
}

local ItemWeights = {
	["Bloxy Cola"] = {
		["weight"] = 0.5
	},
	["Witchs Brew"] = {
		["weight"] = 0.25
	},
	["Goala Cola"] = {
		["weight"] = 0.1
	},
	["Linked Sword"] = {
		["weight"] = 0.3
	},
	["Firebrand"] = {
		["weight"] = 0.1
	},
}

local rarities = {
	["Common"] = {
		["Color"] = Color3.new(0.7, 0.7, 0.7)
	},
	["Uncommon"] = {
		["Color"] = Color3.new(0, 0.666667, 0)
	},
	["Rare"] = {
		["Color"] = Color3.new(0, 0.333333, 1)
	},
	["Epic"] = {
		["Color"] = Color3.new(0.666667, 0, 0.498039)
	},
	["Legendary"] = {
		["Color"] = Color3.new(1, 0.666667, 0)
	},
}



local Speed = 10
local SpeedIncrement = 0.25
local move = false

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		coroutine.wrap(movementOther)(char)
		
		local inventory = Instance.new("Folder", plr)
		inventory.Name = "Inventory"
		local equippedItem = Instance.new("ObjectValue", plr)
		equippedItem.Name = "HeldItem"
	end)
end)



events.Movement.OnServerEvent:Connect(function(plr, movement)
	local char = plr.Character
	char.Animate.walk.WalkAnim.AnimationId = "rbxassetid://94996757896748"
	--char.Animate.run.RunAnim.AnimationId = "rbxassetid://18459825674"
	--local runTrack = char.Humanoid:LoadAnimation(script.Animation)
	--char.Humanoid:LoadAnimation(script.Walk):Play(0.5)
	if movement == "Began" then
		char.Humanoid.WalkSpeed = 0
	end
	if movement == "Ended" then
		for i,v in char.Humanoid:GetPlayingAnimationTracks() do
			v:Stop(1)
		end
	end
	if movement == "Sprint" then
		char.Humanoid:LoadAnimation(script.Anims.Run):Play(1)
		repeat
			Speed += 0.5
			task.wait()
		until Speed == 18
		Speed = 18
	end
	if movement == "SprintEnded" then
		for i,v in char.Humanoid:GetPlayingAnimationTracks() do
			if v.Name == "Run" then
				v:Stop(1)
			end
		end
		repeat
			Speed -= 0.5
			task.wait()
		until Speed == 10
		char.Animate.walk.WalkAnim.AnimationId = "rbxassetid://94996757896748"
		Speed = 10
	end
end)

function movementOther(chara)
	while task.wait() do
		chara.Humanoid.WalkSpeed += SpeedIncrement * 2
		if chara.Humanoid.WalkSpeed > Speed then
			chara.Humanoid.WalkSpeed = Speed
		end
	end
end

local function generateRarity(Rarities, random)
	local totalWeight = 0

	-- get the total weight

	for _, rarity in Rarities do
		totalWeight += rarity.weight
	end

	-- choose a weight
	local chosenRarity = random:NextNumber(0, totalWeight)

	-- find a rarity that matches said weight
	for rarityName, rarity in Rarities do
		local calculated = rarity.weight

		if chosenRarity <= calculated then
			return rarityName
		end

		chosenRarity -= calculated
	end
end

function equipItem(plr, item)
	for i,v in plr.Character.Humanoid:GetPlayingAnimationTracks() do
		--if v.Name == "Grip" then
			v:Stop()
		--end
	end
	if plr.HeldItem.Value ~= nil then
		plr.HeldItem.Value.Parent = plr.Inventory
	end
	
	plr.HeldItem.Value = item
	
	item.Parent = plr.Character
	item.Grip.Part0 = plr.Character["Right Arm"]
	
	script.Universal.AnimationId = itemInfo[item.Name].Animation
	local Grip = plr.Character.Humanoid:LoadAnimation(script.Universal)
	Grip:Play(0.25)
end

function loadItems()
	local function generateItem(itemTemplate)
		local selectedItem = items[generateRarity(ItemWeights, Random.new())]

		local newItem = selectedItem:Clone()
		newItem.CFrame = itemTemplate.CFrame
		newItem.Parent = workspace
		newItem.Anchored = true

		local randomNum = Random.new()

		local rarity = generateRarity(itemInfo[newItem.Name].Rarities, randomNum)

		local proximityPrompt = Instance.new("ProximityPrompt", newItem)
		proximityPrompt.ActionText = "Collect " .. newItem.Name
		proximityPrompt.ObjectText = newItem.Name .. " (" .. rarity .. ")"

		local rarityParticle = particles.Rarity:Clone()
		rarityParticle.Parent = newItem
		rarityParticle.Color = ColorSequence.new(rarities[rarity].Color)

		proximityPrompt.Triggered:Connect(function(plr)
			if #plr.Inventory:GetChildren() < 4 then
				events.ItemCollected:FireClient(plr, itemInfo[newItem.Name], rarity)
				proximityPrompt:Destroy()
				rarityParticle.Enabled = false
				newItem.Anchored = false
				equipItem(plr, newItem)
			end
		end)
	end
	
	local maxRarity = 1
	for i=1, #itemTemplates:GetChildren() do
		local itemTemplate = itemTemplates:GetChildren()[math.random(1, #itemTemplates:GetChildren())]
		local rng = math.random(1, maxRarity)
		if rng <= 3 then
			maxRarity += 1
			generateItem(itemTemplate)
		end
		itemTemplate:Destroy()
	end
end

task.wait()

loadItems()

Help would really be appreciated here and thanks in advance!

1 Like

what i would do is store a string value in the slot frame or something to know what tool it has if you dont already, and set the slots name to its number, so you can use a function like this to easily find it

by checking the input keycode with userInputService and turning that into a number if its in the table. srry if im confusing, goodluck!

local function HotbarInput(input,MenuInput) 
 
    if MenuInput then
        return
    end

	local Enums = {[Enum.KeyCode.One] = 1, [Enum.KeyCode.Two] = 2, [Enum.KeyCode.Three] = 3, [Enum.KeyCode.Four] = 4, [Enum.KeyCode.Five] = 5, [Enum.KeyCode.Six] = 6}
	local KeyCode = Enums[input.KeyCode]

	if KeyCode then -- check if input keycode has its own slot number in the table
		
		local slotFrame = parentofSlots[tostring(KeyCode)] -- find a slot frame named the number "1"  or whatever number the keycode has in the table
		-- code for equiping it here (remote event ask server maybe)
	end

end

UserInputService.InputBegan:Connect(HotbarInput)


--[[
      or u could reference the slot directly if u dont wanna do it the other way
	local KeycodeSlots = {[Enum.KeyCode.One] = slotFrameObjectHere}
      if KeycodeSlots[input.keycode] then
            local slotTriggeredFrame = KeycodeSlots[input.keycode]
      end
]]


--server code, might not work

ARemoteEvent.OnServerEvent:Connect(function(Player, ToolEquipping) --name of tool

	if player.Character:FindFirstChildOfClass("Tool") then
		local currentEquip = player.Character:FindFirstChildOfClass("Tool")

		if currentEquip.Name ~= ToolEquipping then -- so u can swap tools instead of unequipping current one to switch

			if player.Backpack:FindFirstChild(ToolEquipping) then
				player.Backpack:FindFirstChild(ToolEquipping).Parent = player.Character
			end

		end
		currentEquip.Parent = player.Backpack

		--move anything equipped to backpack
	else

		if player.Backpack:FindFirstChild(ToolEquipping) then
			--move tool equipping to player.character

player.Backpack:FindFirstChild(ToolEquipping).Parent = player.Character
		
		end
	end
end)


1 Like

This does sort of help so I’ll go ahead and mark it as a solution.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.