What do you want to achieve? I’m making a fighting game
What is the issue? I’m having a problem with “UserInputService”
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)
PunchTrack:Play()
end
end)
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
Char:FindFirstChild('Humanoid')
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.
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.