You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I’m making a custom hotbar for my roblox horror game. -
What is the issue? Include screenshots / videos if possible!
I have gotten the button to work withMouseButton1Click
, but cannot get it to work with keyboard, eg:Enum.KeyCode.One
. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried asking ChatGPT
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local Tool = script.Parent.ConnectedTool.Value
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local key = script.Parent.ToolNumber.Value -- This is a NumberValue, like 1, 2, etc.
local UIS = game:GetService("UserInputService")
-- Function to equip or unequip the tool
local function toggleTool()
if Tool.Parent ~= char then
hum:EquipTool(Tool)
else
hum:UnequipTools()
end
end
-- Mouse click toggle
script.Parent.MouseButton1Click:Connect(function()
toggleTool()
end)
-- Key press toggle based on ToolNumber value
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end -- Ignore if the game has already processed the input
if input.UserInputType == Enum.UserInputType.Keyboard then
local keyPressed = input.KeyCode.Name -- Get the name of the key (e.g., "One", "Two", etc.)
-- If the key matches the ToolNumber (converted to key name)
if tonumber(keyPressed:match("%d+")) == key then
toggleTool()
end
end
end)
-- Ensure the Humanoid exists if the character respawns
plr.CharacterAdded:Connect(function(newChar)
char = newChar
hum = char:WaitForChild("Humanoid")
end)