Workspace.ScytheSlayin.LocalScript:8: attempt to index nil with 'InputBegan'

Hello! So I was trying to make a keybind script, so when you press a key the animation plays. I tried making it and it didnt work. This is the code I used:

local UIS game:GetService('UserInputService')

local plr = game.Players.LocalPlayer

local Char = plr.Character or plr.CharacterAdded:Wait()

local Key = 'Q'

local Animation = Instance.new("Animation")

local UIS game:GetService('UserInputService')

local plr = game.Players.LocalPlayer

local Char = plr.Character or plr.CharacterAdded:Wait()

local Key = 'Q'

local Animation = Instance.new("Animation")

Animation.AnimationId = 'rbxassetid://5225712430'

local Debounce = true

UIS.InputBegan:Connect(function(Input, IsTyping)

if IsTyping then return end

local KeyPressed = Input.KeyCode

if KeyPressed == Enum.KeyCode[Key] and Debounce then

Debounce = false

local LoadAnimation = Char.Humanoid:LoadAnimation(Animation)

LoadAnimation:Play()

wait(2)

Debounce = true

end

end)

And I keep getting the error:

Workspace.ScytheSlayin.LocalScript:8: attempt to index nil with ‘InputBegan’

(yes I know the indenting is off, I don’t know why it pasted like that)

Any help or idea on what I can do to solve this?

You’re missing an equal sign to assign UserInputService to your UIS field.

However, this is valid syntax which is why there’s no error. UIS is unassigned, and you proceed to call :GetService() to retrieve UserInputService, which returns it to nowhere.

6 Likes