Attempt to index nil with 'UserInputType'

Hello Devs!

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

This is supposed to be a custom hotbar and binding it with keys. Output suggests that I have a problem at line 8.

Thanks!

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.

Direct Connect:

UserInputService.InputBegan:Connect(onInput)

Connect function into Event and Get Parameters:

UserInputService.InputBegan:Connect(function(Input, Processed)
     onInput(Input, Processed)
end)
1 Like

Hm thats true pretty sure it would work if I put parameters there but Im not sure of what it can be… pretty new to UserInputService

Read what @MeCrunchAstroX said,
you didnt call the UIS service and its event [InputBegan]

If you made a function and put some parameters, and you wanna call it, there are 2 ways

1- call it with those arguments,
2-connect it to an event, like here.

1 Like

I only knew the first one, thank you!

You can store your weapons alongside their keys in this table, and shorten your code :slight_smile:

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