How do I add a reload animation to this gun script?

Hey there fellow developers! Can you guys help me?

  1. What do you want to achieve?
    I want to add a reload animation to this script, I was following a tutorial just so you know.

  2. What is the issue?
    I’m not a very good scripter and I’m still learning. Anyways, I can’t add a Reload Animation to this script.

  3. What solutions have you tried so far?
    I tried trying to add one myself by following tutorials from other videos and inserting those scripts into the this script but I didn’t work.

--Variables
local gun = script.Parent
local gun_shot = gun.Handle['Pistol Shoot']
local empty_sound = gun.Handle["Dry Fire"]
local reload_sound = gun.Handle["Pistol Reload"]


--UserInputService Setup
local userInput = game:GetService('UserInputService')

--MouseIcon

--Remote Event Setup
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')

--Mouse Click Check
gun.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if gun.Ammo.Value > 0 then
		remoteEvent:FireServer(gun.Handle.Position, mouse.Hit.p)
		gun_shot:Play()
			gun.Ammo.Value = gun.Ammo.Value - 1
		else
			empty_sound:Play()
		end
	end)
	
end)

--R to Reload Check

userInput.InputBegan:Connect(function(input, gameProcessed)
	
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.R then
				gun.Ammo.Value = 0
				reload_sound:Play()
				reload_sound.Ended:Wait()
				gun.Ammo.Value = 6
				
			end
		end
	end
end)

Huge thanks if you can help - VeilStrife

4 Likes

you could try this

local ReloadAnim -- at the top of the script where variables are
local localPlayer = game.Players.LocalPlayer 
local character = localPlayer.Character --[[ im assuming this is a localscript
if its not then dont do this]]

-- in the reload function
ReloadAnim = character:FindFirstChild("Humanoid"):LoadAnimation(gun.[name of your animation])
	if ReloadAnim then ReloadAnim:Play() end
4 Likes
local tool = script.Parent
local humanoid
local animator

local reload: AnimationTrack -- typing : AnimationTrack in not necessary

tool.Equipped:Connect(function() -- set humanoid once the tool is equipped
humanoid = tool.Parent:FindFirstChild("Humanoid") -- define both user's humanoid and animator
animator = humanoid:WaitForChild("Animator")

reload = animator:LoadAnimation(reloadAnim) -- "reloadAnim" must be an Animation object. 
end)
-- Place this anywhere in the 'reload' function.
reload:Play()

(Please read about Animator object)

2 Likes

i got a error saying: Workspace.VeilStrife.Tool.LocalScript:55: Expected ‘)’ (to close ‘(’ at line 37), got ‘end’

replace this with the path to your animation and remove the brackets

now there’s a errorsaying this : ReloadAnim is not a valid member of Part “Workspace.VeilStrife.Tool.Handle”

can you show me the hierarchy of the tool

here
V Tool
Ammo (Int Value)
LocalScript
V Handle
Dry Fire (Audio)
Pistol Reload (Audio)
Pistol Shoot (Audio)
Touch Interest
Reload Anim (Animation)

note: i had to write it in text cause it couldnt upload images :neutral_face:

if the name of the animation has a space in it just do Handle["Reload Anim"] like you did at the top of the script

why is the animation inside of the handle?!?!?!

oh sorry is it supposed to be in the script or tool?

well its usually in the tool but you can put it wherever you want, but the handle is mainly used for sounds and stuff like that

ah thanks for the info, i’ll try doing what you said

it works!!! thanks for the help man!

you are welcome but also, the post below mine’s code is more up-to-date as it uses the Animator object (something i never used or heard of before) so you should find out how you can use his code in the future

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.