I'm making a gun that loads animations, and when I first use the gun, the animations work fine. However, when I reset and use the gun again, the animations don't work and instead throw me an error. I've looked everywhere on the internet for the same issue and they don't work for me.
The error is on a ModuleScript, and is on this line:
local animTrack = animator:LoadAnimation(animation)
If the whole script helps then:
local gunScripts = {} gunScripts.PistolScript = function(GunName) --Variables local runService = game:GetService("RunService") local UIS = game:GetService("UserInputService") local player = game.Players.LocalPlayer local mouse = player:GetMouse() local char = workspace:WaitForChild(player.Name) local gun = player:WaitForChild("Backpack"):WaitForChild(GunName) or player.Character:WaitForChild(GunName) local playerGui = player:WaitForChild("PlayerGui") local mobileReloadGui = playerGui:WaitForChild("MobileReloadGui") local mobileReloadButton = mobileReloadGui:WaitForChild("ReloadButton") local ammoGui = playerGui:WaitForChild("AmmoGUI") local ammoText = ammoGui.Background.AmmoText local gunLabel = ammoGui.GunLabel local ReloadGui = playerGui:WaitForChild("ReloadGui") local reloadText = ReloadGui.ReloadText local rs = game:GetService("ReplicatedStorage") local re = rs:WaitForChild("Events"):WaitForChild("ShootEvent") local weaponInfo = require(rs.Modules.WeaponInfo) local gunIndex = weaponInfo:GetWeaponByName(GunName) local debounce = false local firerate = gunIndex.Firerate local GunAnimations = rs.GunAnimations local animator = player.Character.Humanoid:FindFirstChild("Animator") local ammoAmount = gunIndex.Ammo local connection local firing = false runService.Heartbeat:Connect(function() ammoText.Text = "Ammo: "..ammoAmount.."/"..gunIndex.Ammo end) if not UIS.KeyboardEnabled then gun.Equipped:Connect(function() if ammoAmount < gunIndex.Ammo then mobileReloadGui.Enabled = true end end) gun.Unequipped:Connect(function() mobileReloadGui.Enabled = false end) mobileReloadButton.MouseButton1Click:Connect(function() local reloadDebounce = false if reloadDebounce then return end reloadDebounce = true local reloadAnim = GunAnimations:WaitForChild("PistolReload") local reloadTrack = gunScripts.loadAnimation(animator, char:FindFirstChildWhichIsA("Humanoid"), reloadAnim) reloadTrack:Play() local reloadSound = gun:WaitForChild("ReloadSound") reloadSound:Play() local isUnequipped = false gun.Unequipped:Connect(function() isUnequipped = true if reloadTrack.IsPlaying then print("stopped") reloadTrack.TimePosition = 0 reloadTrack:Stop() reloadSound:Stop() reloadDebounce = false end end) reloadTrack.Ended:Connect(function() if not isUnequipped then print("reloaded") ammoAmount = gunIndex.Ammo print("ammount = 19") ReloadGui.Enabled = false reloadDebounce = false end end) end) end function gunScripts.loadAnimation(animator, humanoid, animation) if not animator then animator = Instance.new("Animator") if humanoid.Parent.Parent == nil then repeat task.wait(1) until humanoid.Parent.Parent == workspace end animator.Parent = humanoid end print("Parent: "..humanoid.Parent.Name) print("Animator: "..animator.Name) print("Animation: "..animation.Name) local animTrack = animator:LoadAnimation(animation) print(animTrack) return animTrack end --When activated, it will fire remote event. function gunScripts.onActivated() --If the equip animation is done and if the player has more than 0 ammo then... if gun:GetAttribute("PlayedEquipAnimation") and ammoAmount > 0 then --The shooting animation for the gun. local shootAnim = GunAnimations.PistolShoot --If the character's humanoid does not have a humanoid to load animations in then: local shootTrack = gunScripts.loadAnimation(animator, char:FindFirstChildWhichIsA("Humanoid"), shootAnim) shootTrack:Play() --Sets the target of the player's gun to the mouse's target. local params = RaycastParams.new() params.FilterDescendantsInstances = { gun, player.Character } params.FilterType = Enum.RaycastFilterType.Exclude params.IgnoreWater = true local mouseRay = game.Workspace.Camera:ScreenPointToRay(mouse.X, mouse.Y) local origin = mouseRay.Origin local direction = mouseRay.Direction * 500 local result = workspace:Raycast(origin, direction, params) local target = nil if result and result.Instance then target = result.Instance end print(target) --Fires a remote event to damage the enemy. re:FireServer(target, result) --Subtracts the ammo amount variable by 1. ammoAmount -= 1 --Sets the GUI text to display the amount of ammo. ammoGui.Background.AmmoText.Text = "Ammo: "..ammoAmount.."/"..gunIndex.Ammo --If the ammo amount is 0, however, then the reloadGUI displays. if ammoAmount == 0 then ReloadGui.Enabled = true end end end --When the gun is equipped then, gun.Equipped:Connect(function() print("equipped") --The display text for the gunGUI displays the gun's name. gunLabel.Text = gun.Name --Allows the player to see the ammoGUI ammoGui.Enabled = true --if ammo amount is 0 then if ammoAmount == 0 then --The player can see the reload gui ReloadGui.Enabled = true end --The equip animation. local equipAnim = GunAnimations:WaitForChild("PistolEquip") local equipAnimTrack = gunScripts.loadAnimation(animator, char:FindFirstChildWhichIsA("Humanoid", true), equipAnim) equipAnimTrack.Looped = false equipAnimTrack:Play() gun:SetAttribute("PlayedEquipAnimation", false) equipAnimTrack.Ended:Connect(function() gun:SetAttribute("PlayedEquipAnimation", true) end) local holdAnim = GunAnimations:WaitForChild("PistolHold") local holdAnimTrack = gunScripts.loadAnimation(animator, char:FindFirstChildWhichIsA("Humanoid", true), holdAnim) holdAnimTrack.Priority = Enum.AnimationPriority.Action holdAnimTrack.Looped = true holdAnimTrack:Play() --If the gun is activated, connect to a function mouse.Button1Down:Connect(function() if not firing and gun:GetAttribute("PlayedEquipAnimation") then firing = true while firing == true do gunScripts.onActivated() wait(firerate) end end end) gun.Deactivated:Connect(function() firing = false end) UIS.InputBegan:Connect(function(input, GPE) if not GPE then if input.UserInputType == Enum.UserInputType.Touch then if not firing and gun:GetAttribute("PlayedEquipAnimation") then firing = true while firing == true do gunScripts.onActivated() wait(firerate) end end end end end) UIS.InputEnded:Connect(function(input, GPE) if not GPE then if input.UserInputType == Enum.UserInputType.Touch then firing = false end end end) --When the gun is unequipped, gun.Unequipped:Connect(function() --disable the ammoGUI ammoGui.Enabled = false --disable the reloadGUI ReloadGui.Enabled = false --If the animation is still playing after the gun is unequipped, if equipAnimTrack.IsPlaying then gun:SetAttribute("PlayedEquipAnimation", false) --Stop the track equipAnimTrack:Stop() --Restart the time position. equipAnimTrack.TimePosition = 0 end holdAnimTrack:Stop() holdAnimTrack.TimePosition = 0 --Set playedequipanimation to false gun:SetAttribute("PlayedEquipAnimation",false) end) end) UIS.InputBegan:Connect(function(input, GPE) local reloadDebounce = false if not GPE then if input.KeyCode == Enum.KeyCode.R and ammoAmount < gunIndex.Ammo and reloadDebounce == false and gun:GetAttribute("PlayedEquipAnimation") then reloadDebounce = true local reloadAnim = GunAnimations:WaitForChild("PistolReload") local reloadTrack = gunScripts.loadAnimation(animator, char:FindFirstChildWhichIsA("Humanoid"), reloadAnim) reloadTrack:Play() local reloadSound = gun:WaitForChild("ReloadSound") reloadSound:Play() local isUnequipped = false gun.Unequipped:Connect(function() isUnequipped = true if reloadTrack.IsPlaying then print("stopped") reloadTrack.TimePosition = 0 reloadTrack:Stop() reloadSound:Stop() reloadDebounce = false end end) reloadTrack.Ended:Connect(function() if not isUnequipped then print("reloaded") ammoAmount = gunIndex.Ammo print("ammount = 19") ReloadGui.Enabled = false reloadDebounce = false end end) end end end) end return gunScripts
As always, thanks for your time.