FPS ViewModel Not Playing Animations

Hello fine developers of the DevForums,

I’ve recently once again ran into another problem. I’ve recently gotten into developing a gun engine system but the problem is, the viewmodel isn’t playing any animations. It’s just stuck.

Video

robloxapp-20200916-1000046.wmv (2.2 MB)

Here is the code:

-------------
--- INDEX ---
-------------

-- SERVICES

local Players = game:GetService("Players");
local RunService = game:GetService("RunService");
local uis = game:GetService("UserInputService");
local ReplicatedStorage = game:GetService("ReplicatedStorage");

-- VARIABLES

local client = Players.LocalPlayer;
local character = client.Character or client.CharacterAdded:Wait();
local viewmodel = ReplicatedStorage:WaitForChild("Viewmodel2"):Clone();
viewmodel.Parent = workspace
local viewHum = viewmodel:WaitForChild("Humanoid");
local humanoid = character:WaitForChild("Humanoid");
local camera = workspace.CurrentCamera;

while character.Parent == nil do
	character.AncestryChanged:Wait()
end
-- ANIMATIONS
local IdleAnim = viewHum:LoadAnimation(viewHum.Parent:WaitForChild("Idle"))
local FireAnim = viewHum:LoadAnimation(viewHum.Parent:WaitForChild("Fire"))
local ReloadAnim = viewHum:LoadAnimation(viewHum.Parent:WaitForChild("Chamber"))


-- BOOLEANS

local canShoot = true

-- RECOIL

local RecoilPattern = {
	{3, 12, -1, 0.77, -0.1},
	{6, 12, -1, 0.77, 0.1},
	{8, 12, -1, 0.77, -0.1},
	{10, 12, -1, 0.77, 0.1},
}
local RecoilReset = 0.5 -- Time it takes for recoil pattern to reset back to 1

local curshots = 0
local lastclick = tick()

function lerp(a, b, t) -- Gets a number between two points using an alpha
	re    turn a * (1 - t) + (b * t)
end

local function ShootRecoil()
	curshots = (tick() - lastclick > RecoilReset and 1 or curshots + 1) -- Either reset or or increase the current shot we're at
	lastclick = tick()
	for i, v in pairs(RecoilPattern) do
		if curshots <= v[1] then -- Found the current recoil we're at
			spawn(function()
				local num = 0
				while math.abs(num - v[2]) > 0.01 do
					num = lerp(num, v[2], v[4])
					local rec = num / 10
					camera.CFrame = camera.CFrame * CFrame.Angles(math.rad(rec), math.rad(rec * v[5]), 0)
					RunService.RenderStepped:Wait()
				end
				while math.abs(num - v[3]) > 0.01 do
					num = lerp(num, v[3], v[4])
					local rec = num / 10
					camera.CFrame = camera.CFrame * CFrame.Angles(math.rad(rec), math.rad(rec * v[5]), 0)
					RunService.RenderStepped:Wait()
				end
			end)
			break
		end
	end
end
---------------------
--- INTIALIZATION ---
---------------------


IdleAnim:Play()
IdleAnim.Looped = true

RunService.RenderStepped:Connect(function()
	viewmodel:WaitForChild("Head").CFrame = camera.CFrame
end)

humanoid.Died:Connect(function()
	viewmodel.Parent = nil;
end)

ReloadAnim.Stopped:Connect(function()
	canShoot = true
end)

uis.InputBegan:Connect(function(key)
	if key.UserInputType == Enum.UserInputType.MouseButton1 then
		if canShoot then
			FireAnim:Play();
			ShootRecoil();
			print("Shots Fired");
		end
	elseif key.KeyCode == Enum.KeyCode.R then
		canShoot = false;
		ReloadAnim:Play();
		print("Reload");
	end
end)

The script is inside StarterPlayerScripts.

Can you please select all your code then click this(Screenshot by Lightshot) so its easier to read.

1 Like

Did it, there we go. Hope you can read it properly now.

You could probably do it this way:

local VMAnim= Instance.new('Animation')
VMAnim.AnimationId = 'rbxassetid://6056010029'
VMShootAnim= game.Workspace.CurrentCamera.yourgun.Animation:LoadAnimation(VMAnim)

(add an animation controller if it doesn’t have a humanoid)