Need help on reload animation for gun

So, I made a script that requires a player to press r to reload a gun. But it keeps playing again even if I put AnimationTrack.Looped = false in the script.

  1. What do you want to achieve? Not play the animation after playing once.

  2. What is the issue? The animation keeps repeating.

  3. What solutions have you tried so far? Editing the script, searched for YouTube videos but none showed up.

Script for the gun animation:

local Key = "R"
local Player = game.Players.LocalPlayer
local Character = Player.Character
local AnimationID = "6587068338"
local CharAnimation
game:GetService("UserInputService").InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode["R"] then
		animation()
	end
end)
function animation()
	if Character then
		local Animation = Character:FindFirstChild("AnimationCharacter")
		if CharAnimation then
			CharAnimation:Stop()
		end
		if Animation then
			if Animation.AnimationId=="rbxassetid://"..AnimationID then
				Animation:Destroy()
				return
			end
			Animation:Destroy()
		end
		local AnimationTrack =Instance.new("Animation",Character)
		AnimationTrack.Name= "AnimationCharacter"
		AnimationTrack.AnimationId = "rbxassetid://6587068338"..AnimationID
		CharAnimation = Character.Humanoid:LoadAnimation(AnimationTrack)
		CharAnimation:Play()
		AnimationTrack.Looped = false
	end
end

Video of the problem:
robloxapp-20210330-1037495.wmv (1.3 MB)

Btw, I used @Disobeyedcrab04’s map. I am still learning to script, that’s why I am using it lol.

AnimationTrack.AnimationId = "rbxassetid://6587068338"..AnimationID

  1. You wouldn’t need to add the ..AnimationID since you already defined the asset ID. Or you could remove the ID in the quotations and leave the rest of that line the same.

  2. Set the looped value to false before loading the animation, and if it isn’t already off then turn off the loop setting in the animation editor.

2 Likes

I see a couple of problems here.

If the

"rbxassetid://"

is causing the problem, try using this.

"http://www.roblox.com/asset/?id="

Looking at the bottom,

		CharAnimation:Play()
		AnimationTrack.Looped = false

You will want to put the looped before the animation plays.

2 Likes

Alright, I did the things you guys said, but it still didn’t work sadly.

1 Like

Try this.

local key = Enum.KeyCode.R --Keybind
local players = game:GetService("Players")
local player = players.LocalPlayer 
local character = player.Character or player.CharacterAdded:Wait()
local uis = game:GetService("UserInputService")

local animID = "" --Your anim here

local start = "http://www.roblox.com/asset/?id=" -- starter
local CurrentAnim --PlayingAnim

local function PlayAnim()
	if CurrentAnim then --Geting ri of the old anims
		CurrentAnim:Stop()
		CurrentAnim:Destroy()
	end
	
	local na = Instance.new("Animation") --making the new anim
	na.AnimationId = start .. animID
	
	local animLoad = character:WaitForChild("Humanoid"):LoadAnimation(na) --loading to humanoid
	CurrentAnim = animLoad --seting new anim as the playing anim
	animLoad.Looped = false -- looped se tto off
	animLoad:Play() --playing
	na:Destroy() --destroys the animation obj
end

uis.InputBegan:Connect(function(i)
	if i.KeyCode == key then --checks if it was the right key
		PlayAnim() --plays anim
	end
end)

Change the anim id to just the number. Ex(434345621865)

I realized this just now, you were setting the animation object not when it was loaded. I believe the animation track is when it is loaded to the humanoid, so I changed it to set the loop of when it was loaded, if that makes sense.

3 Likes

Sorry, late reply. Anyways, where do I put the script? It’s a local script right? Sorry if I’m asking too much questions.

Where you had your original script.

1 Like

Alright, sadly it still didn’t work. I tried to change the keybinds, and still, nothing.

So that animation doesn’t play?

If you are using Roblox’s animator there is a setting called SetAnimationPriority under the three dots that’s next to the play buttons. Set it to Action if you haven’t already.

Also, if it isn’t in there already put the local script in StarterGui.

I tested my script before and it worked, the looped stopped.

The animation does play, however, it still repeats. The animation is action type. The local script is also in starter gui.

Try importing the animation back into the animation editor, then make it so it doesn’t loop, then overwriting it with the asset you had it as.