I have problem with my code that I can’t figure out. I can’t seem to find the problem.
local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local function onInput(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then -- the problem is here
if input.KeyCode == Enum.KeyCode.One then
player.Backpack.ClassicSword.Parent = player.Character
elseif input.KeyCode == Enum.KeyCode.Two then
player.Backpack.RocketLauncher.Parent = player.Character
elseif input.KeyCode == Enum.KeyCode.Three then
player.Backpack.ClassicSuperball.Parent = player.Character
elseif input.KeyCode == Enum.KeyCode.Four then
player.Backpack.ClassicSlingshot.Parent = player.Character
elseif input.KeyCode == Enum.KeyCode.Five then
player.Backpack.ClassicTimebomb.Parent = player.Character
elseif input.KeyCode == Enum.KeyCode.Six then
player.Backpack.ClassicTrowel.Parent = player.Character
end
end
end
if UserInputService.KeyboardEnabled then
onInput()
end
It’s not working because you didn’t Connect the function into UserInputService. There’s 2 ways of doing it: Directly connecting it or Connecting the Event into a function, Get it’s Parameters and call the function.
You can store your weapons alongside their keys in this table, and shorten your code
local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local myTools = {
["ClassicSword"] = Enum.KeyCode.One;
["RocketLauncher"] = Enum.KeyCode.Two;
}
local function onInput(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard then -- the problem is here
for tool,code in pairs(myTools) do
if input.KeyCode == code then
for _,TOOL in pairs(player.Character:GetChildren()) do
if TOOL:IsA("Tool") then
TOOL:Destroy()
end
end
player.Backpack[tool].Parent = player.Character
end
end
end
end
if UserInputService.KeyboardEnabled then
UserInputService.InputBegan:Connect(onInput)
end