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:
-
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.
-
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.
-
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.
-
wait()
has been superseded by task.wait() and should not be used anymore
-
InputEnded is another event of UserInputService
, you are using the syntax for connecting to this event incorrectly.
-
You’re not using the tool
variable or the reloading
variable
-
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.
-
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?)
-
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
-
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
- Services should be declared at the top of the file
- All services should be accessed via
game:GetService()
- Variables should be named in lowerCamelCase
- Constants should be named in UPPER_SNAKE_CASE
- 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