Viewmodel Animations Not Working

Ive been having a issue with weapon swapping in my game the issue is
that the animations arent loading, weapon swapping works by teleporting the weapon viewmodel to the camera and when you switch weapons you teleports another and teleports the unused viewmodels to cframe.new, when the game starts the animations all get loaded on the animation controller but animation only works on one view model


local Weapon = 2
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game["Players"]["LocalPlayer"]
local ScreenAmmo = Player.PlayerGui.Ammo
local Mouse = Player:GetMouse()
local MouseDown = 0
local Shooting = 0
local MaxAmmo = nil
local FOV = Instance.new("NumberValue")
FOV.Parent = workspace
FOV.Name = "FOV Slider Value"
FOV.Value = 0
local WeaponFolder = ReplicatedStorage.Viewmodels:Clone()
WeaponFolder.Parent=workspace.CurrentCamera
if Weapon == 1 then
	MaxAmmo = 10
else
	MaxAmmo = 20
end
local Ammo = MaxAmmo
local UIS = game:GetService("UserInputService")
local Viewmodel: Model = WeaponFolder["v"..Weapon]
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
local AnimationPlayer = Viewmodel:FindFirstChildOfClass("AnimationController")
local DamageEvent = ReplicatedStorage["DamageEvent"]
local Animations = {
	reload1_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.reload1),
	idle1_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.idle1),
	fire1_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.fire1),
	reload2_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.reload2),
	idle2_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.idle2),
	fire2_animation = AnimationPlayer:LoadAnimation(ReplicatedStorage.Animations.fire2)
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Mouse.Button1Down:Connect(function()
	MouseDown=1
end)
Mouse.Button1Up:Connect(function()
	MouseDown=0
end)
game:GetService("RunService"):BindToRenderStep("Viewmodel", 301, function(DT)
	if Weapon == 1 then
		MaxAmmo = 10
	else
		MaxAmmo = 20
	end
	for i=0,2 do
		if i ~= Weapon then
			local View:Model = WeaponFolder["v"..i]
			View:PivotTo(CFrame.new())
		end
	end
	if Ammo > MaxAmmo then
		Ammo = MaxAmmo
	end
	if Weapon > 2  then
		Weapon = 2
	end
	if Weapon < 0 then
		Weapon = 0
	end
	Viewmodel = WeaponFolder["v"..Weapon]
	AnimationPlayer = Viewmodel:FindFirstChildOfClass("AnimationController")
	Viewmodel:PivotTo(workspace.CurrentCamera.CFrame)
	Viewmodel:PivotTo(Viewmodel.PrimaryPart.CFrame + Viewmodel.PrimaryPart.CFrame.LookVector * FOV.Value)
	ScreenAmmo.TextLabel2.Text = "Weapon:"..Weapon
	Animations["idle"..Weapon.."_animation"]:Play()
	if Shooting == 0 and Ammo > 0 then
		Shooting = 1
		if MouseDown == 1 then
			Animations["fire"..Weapon.."_animation"]:Play()
			local Sound = script.Fire:Clone() Sound.Parent = workspace
			if Mouse.Target then
				if Mouse.Target.Parent:IsA("Model") then
					local Object = Mouse.Target.Parent
					local Humanoid: Humanoid = Object:FindFirstChild("Humanoid")
					if Humanoid then
						DamageEvent:FireServer(Humanoid,10)
					end
				end
				if Mouse.Target.Parent.Name == "Breakable" then
					ReplicatedStorage["UnanchorPart"]:FireServer(Mouse.Target)
				end
			end
			Sound:Play()
			Ammo-=1
			if Ammo < 0 then
				Ammo = 0
			end
			if Weapon == 1 then
				while MouseDown == 1 do
					wait(0.1)
				end
			else
				wait(0.1)
			end
		end
		Shooting = 0
	end
	UIS.InputBegan:Connect(function(key,event)
		if key.KeyCode == Enum.KeyCode.R then
			if Ammo == 0 then
				Shooting = 1
				Animations["reload"..Weapon.."_animation"]:Play()
				local Sound = script.Reload:Clone() Sound.Parent = workspace
				Sound:Play()
				for i=1,MaxAmmo do
					Ammo += 1
					wait(0.2)
				end
				Shooting = 0
			end
		end
	end)
	ScreenAmmo.TextLabel.Text = Ammo.."/"..MaxAmmo
end)
local hum: Humanoid = Player.Character["Humanoid"]
hum.Died:Connect(function()
	Viewmodel:Destroy()
end)
ReplicatedStorage["SentAmmo"].OnClientEvent:Connect(function(num)
	Ammo += num
end)
UIS.InputBegan:Connect(function(key)
	if Shooting == 0 then
		if key.KeyCode == Enum.KeyCode.Q then
			Weapon -= 1
			wait(1)
		end
		if key.KeyCode == Enum.KeyCode.E then
			Weapon += 1
			wait(1)
		end
	end
end)

if thats not enough you can view the whole project here (uncopylocked)

2 Likes

I don’t think your project is uncopylocked.

But don’t make it uncopylocked cuz it’s risky. Just show us what you think is the cause of the problem.

Also you accidentally posted it twice.

Oh Okay Sorry i think the cause of the problem should be in the code i shared because its the main viewmodel script it handles weapon swapping, and moving the viewmodel and stuff do you have any idea how to fix it? (you can play the game to get a better idea of the game the controls are left click to shoot and r to shoot and q and e to scroll thourgh weapons)

I tried it and I can see that other weapons aren’t working.

What if you set the weapon to pistol on the run? Would the AK-47 animations not work?

i think the problem is where it loads animations and moves the viewmodels (maybe its loading all the animations on one animation controller im not sure do you know a way i could load all the animations for all the animation controllers)

oh the weapons arent tools they are just models being moved around should i make them tools?

Shouldn’t you load a new set of animations after theres a change in weapons?

Because those animations above are used for the AK-47.

i tried that didnt work after swapping weapons a litte bit, roblox stop letting me load animations do you know a way to clear animations?

I don’t think you have to clear animations, cuz I searched and there’s no way to clear them.