How do you make a keybind script?

  1. What do you want to achieve? I’m making a fighting game

  2. What is the issue? I’m having a problem with “UserInputService”

  3. What solutions have you tried so far? I tried lo look it up on Youtube but none of the scripts gave me better solutions

Is there any errors I’ve placed? Any code I’ve missed?

local Player = game.Players.LocalPlayer
local Char = Player.Character
local Humanoid = Char.Humanoid
local UIS = game:GetService("UserInputService")
local Punch = script.Punch

UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode.Z then
		local PunchTrack = Humanoid:LoadAnimation(Punch)

Start using debugging tools and use breakpoints

But stop using the UserInputService and use the ContextActionService

Depending on when/where this runs, the player may not have their character yet, or may have respawned and have a different character to the one you’ve stored.

It’s often safer when dealing with characters to grab it when you need it, and also to return out of the handler if the character or humanoid doesn’t exist at the time.

UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
    local Char = Player.Character
    local Humanoid = Char and 
    if not Humanoid then return end

Try put in something before the script like wait(1) or WaitForChild(“Character”). As @BanTech said “the player may not have their character yet”.

You could also try put in something like print(“ActivatingPunch”), so you can track if the error is in the user input code. Let me know if it still doesn’t work :D.

Normally repeat wait() until player.Character is what people do.

I think :WaitForChild("Character") will try to find something that has name of Character inside player, it will not wait for his character

I recommend don’t do :WaitForChild("Character"), it will return error:

Attempt index nil with "WaitForChild("Character")

There’s nothing at all wrong with using UserInputService… CAS is more geared towards situations where action priority is important.


So do I use ContextActionService instead of UserInputService?

No, you should be fine with UserInputService. As someone said, it could be because the player’s Character hasn’t loaded yet. Try:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

-- rest of code

If this still does not work, it can be something wrong with your animation.