Help with reload Keybind

Hello!
I know this probably wasn’t gonna work anyways as I barely understand UIS.
My goal is to just simply make the animation play once UIS keyboard input R was pressed. Then wait 1 second and InputEnded.

Here is my code:

local tool = script.Parent

local reloading = false

local UserInputService = game:GetService("UserInputService")

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


local ReloadAnimShotgun = Instance.new("Animation")
ReloadAnimShotgun.AnimationId = "rbxassetid://10003466909"
local AnimationTrackReloadShotgun = Humanoid.Animator:LoadAnimation(ReloadAnimShotgun)

local ReloadKey = UserInputService.Keycode.R

UserInputService.Keycode.R.InputBegan:Connect(function()
	AnimationTrackReloadShotgun:Play()
	wait(1)
	UserInputService.Keycode.R.InputEnded = true
end)

No Output errors.

UserInputService.InputBegan:Connect(function(Key)
    if Key.KeyCode == Enum.KeyCode.R then
        --do your stuff in here
    end
end)

I would use the function like this instead. This way, if you were to make different types of guns, (persay, one had an alt fire mode), you can simply add another if statement.

And this line: UserInputService.Keycode.R.InputEnded = true doesn’t make that much sense to me. I haven’t seen this before. Is this to notify something that the gun has finished reloading?

1 Like

hmm is still isn’t play the animation, the animation was saved with the gun i created, may that be the reason it cannot play?

I would create a physical animation object and parent it underneath the script, and then load it into the humanoid and play. Hope this helps

1 Like

Okay I am kind of getting it work I just need to adjust the animation weight.

1 Like

Got it to work,
thank you to warrior and warriorgamer for help.

Issues:

  1. InputBegan is an event of UserInputService. You are using incorrect syntax to reference the event (Keycode is not a property of UserInputService). See that link for documentation and code samples of how to properly use it.

  2. The InputBegan event fires for all input, and it passes an InputObject as a parameter, which contains a KeyCode property that you can use to check what kind of input fired this event.

  3. It also passes a gameProcessedEvent boolean to tell you if the game processed the input, such as if the player is typing in chat and presses a key. In such a case when it is true, we want to ignore that input so that, for example, the player won’t reload when they type the letter ‘r’ in a word while trying to chat.

  4. wait() has been superseded by task.wait() and should not be used anymore

  5. InputEnded is another event of UserInputService, you are using the syntax for connecting to this event incorrectly.

  6. You’re not using the tool variable or the reloading variable

  7. player.Character is a reference to a model or nil, it is not an event so you cannot call :Wait() on it. Instead, you should wait for the CharacterAdded event.

  8. You should add a debounce to your input listener to avoid starting a reload while another one is running (probably what your reloading variable was meant to be?)

  9. You might consider waiting for the animation to finish playing rather than waiting a specific amount of time, such as with the AnimationTrack.Stopped method

  10. You don’t really need to worry about when the button is released, i.e. the InputEnded method is irrelevant here

Other coding style changes, per the Roblox Lua Style Guide
  1. Services should be declared at the top of the file
  2. All services should be accessed via game:GetService()
  3. Variables should be named in lowerCamelCase
  4. Constants should be named in UPPER_SNAKE_CASE
  5. I like to prefix my boolean variables with words like is for clarity on what the variable represents
--!strict

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local RELOAD_KEY = UserInputService.Keycode.R

local localPlayer = Players.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait() 
local humanoid = character:WaitForChild("Humanoid")
local isReloading = false

local reloadShotgunAnimation = Instance.new("Animation")
reloadShotgunAnimation.AnimationId = "rbxassetid://10003466909"
local reloadShotgunAnimationTrack = humanoid.Animator:LoadAnimation(reloadShotgunAnimation)

UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if gameProcessedEvent then
		return
	end

	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == RELOAD_KEY then
			if isReloading then
				return
			end
	
			isReloading = true
			reloadShotgunAnimationTrack:Play()
			reloadShotgunAnimationTrack.Stopped:Wait()
			isReloading = false
		end
	end
end)
1 Like