Viewmodel equip animation not work in the right way


IDK WHY WHEN I PULL MY WEAPON OUT THE FIRST TIME IT LOOK LIKE THIS
MY LOADSLOTSCRIPT:

function loadSlot(itemName)
	
	local viewmodelFolder = game.ReplicatedStorage:WaitForChild("WeaponViewmodel")
	local moduleScript = moduleFolder:FindFirstChild(itemName)
	if not moduleScript then return end
	
	for _, v in ipairs(camera:GetChildren()) do
		if v:IsA("Model") then 
			if equipAnim then equipAnim:Stop() end
			if reloadAnim then reloadAnim:Stop() end
			if emptyReloadAnim then emptyReloadAnim:Stop() end
			if idleAnim then idleAnim:Stop() end
			if deequipAnim then deequipAnim:Play() end
			repeat task.wait() until deequipAnim.IsPlaying == false
			v:Destroy()
		end
	end

	local success, settings = pcall(require, moduleScript)
	if not success then return end
	framework.module = settings

	local vm = viewmodelFolder:FindFirstChild(itemName)
	if vm then
		local clone = vm:Clone()
		clone.Parent = camera
		framework.viewmodel = clone

		if clone:FindFirstChild("AnimationController") then
			
			
			
			local function createAnim(id)
				

				local anim = Instance.new("Animation")
				

				local success, loadedAnim = pcall(function()
					anim.AnimationId = id
					return clone.AnimationController.Animator:LoadAnimation(anim)
				end)

				if not success then
					warn("āš ļø Failed to load animation:", id, "→", loadedAnim)
					return nil
				end

				return loadedAnim
			end

			print(settings)
			fireAnim = createAnim(settings.fireAnim)
			emptyfireAnim = createAnim(settings.emptyFireAnim)
			equipAnim = createAnim(settings.equipAnim)
			deequipAnim = createAnim(settings.deequipAnim)
			reloadAnim = createAnim(settings.reloadAnim)
			emptyReloadAnim = createAnim(settings.emptyReloadAnim)
			idleAnim = createAnim(settings.idleAnim)
			local success, loadedAnim = pcall(function()
			local tpIdleAnim = Instance.new("Animation")
			tpIdleAnim.Parent = player.Character
			tpIdleAnim.Name = "IdleAnim"
			tpIdleAnim.AnimationId = framework.module.tpidleAnim
			tpIdleAnim = character.Humanoid.Animator:LoadAnimation(tpIdleAnim)
			tpIdleAnim:Play(framework.module.tpidleAnim)
				tpShootAnim = Instance.new("Animation") -- line 170
				tpShootAnim.Parent = player.Character
				tpShootAnim.Name = "TpShootAnim"
				tpShootAnim.AnimationId = framework.module.tpshootAnim
				tpShootAnim = character.Humanoid.Animator:LoadAnimation(tpShootAnim)
			end)
		end
		if framework.viewmodel then
			for i, v in pairs(framework.viewmodel:GetDescendants()) do
				if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("BasePart") then
					v.Transparency = 1
				end
			end
		end
		if equipAnim then
		equipAnim:Play()
		end

		task.wait(.1)

		if framework.viewmodel then
			for i, v in pairs(framework.viewmodel:GetDescendants()) do
				if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("BasePart") then
					if v.Name == "Main" or v.Name == "Muzzle" or v.Name == "FakeCam" or v.Name == "Aimpart" or v.Name == "HumanoidRootPart" then

					else
						v.Transparency = 0
					end
				end
			end
		end

		local torso = character:FindFirstChild("UpperTorso")
		if torso then
			if torso:FindFirstChild("FireSound") then torso.FireSound:Destroy() end
			if settings.fireSound then
				GunEvent.LoadSlot:FireServer(settings.fireSound.SoundId, settings.fireSound.Volume,itemName)
			end
		end
	end
	local mouse = player:GetMouse()

	mouse.Icon=framework.module.MouseCursor
end

What exactly is the issue here? You need to show or at least tell what you are expecting to see.

u see when i pull my gun the first time? it appear before my animation play

I’m not sure if i can see it (possibly video compression) but I had a similar issue with my own weapon system. My solution was to hide the viewmodel and wait until the animations have loaded.

Hiding/Showing Viewmodel:

local function setViewmodelVisible(viewmodel, visible)
	local transparency = visible and 0 or 1
	if viewmodel == nil then return end
	for index, descendant in ipairs(viewmodel:GetDescendants()) do
		if descendant:IsA("BasePart") then
			descendant.LocalTransparencyModifier = transparency
		end
	end
end

And to wait for the animation to load:

repeat task.wait() until Animations.firstPerson.initialpullout.Length > 0

I hope this helps (:

1 Like