Difficulty with equip and unequp animations to viewmodel

I’m having trouble with the equip and unequip animations of my viewmodel, the animation that i made for them is a simple linear move of the arm’s going up or down, but in runtime, it starts at the default position, then tries to tween with the equip or unequip animations, im a bit lost on what is going on, could someone explain to me?

-- services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
-- variables
local player = Players.LocalPlayer
local tool = script.Parent
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid") :: Humanoid
local animator = humanoid:WaitForChild("Animator") :: Animator
local viewModel = ReplicatedStorage.Assets.ViewModels.Knife

local swung = tool.Swung
local properties = require(tool.Properties)
local rng = Random.new()

local swingDebounce = false
local equipped = false

-- functions

tool.Handle.Transparency = 1
player.CameraMode = Enum.CameraMode.LockFirstPerson

local function createTrack(animID, track) : AnimationTrack
	local animation = Instance.new("Animation")
	animation.AnimationId = animID
	return track:LoadAnimation(animation)
end

local equipTrack = createTrack(properties.EquipAnimation, animator)
equipTrack:SetAttribute("ViewModelAnimation", true)

tool.Equipped:Connect(function()
	equipped = true
	viewModel = ReplicatedStorage.Assets.ViewModels.Knife:Clone()
	viewModel.Parent = camera
	UserInputService.MouseIconEnabled = false
	local viewModelTrack = createTrack("rbxassetid://18628296979", camera.Knife.Humanoid.Animator)
	equipTrack:Play()
	viewModelTrack:Play()
	
end)

tool.Unequipped:Connect(function()
	local viewModelTrack = createTrack("rbxassetid://18628399511", camera.Knife.Humanoid.Animator)
	viewModelTrack:Play()
	viewModelTrack.Ended:Wait()
	equipped = false
	equipTrack:Stop()
	camera.Knife:Destroy()
	UserInputService.MouseIconEnabled = true
end)


tool.Activated:Connect(function()
	if swingDebounce then
		return
	end
	swingDebounce = true
	
	local swingTrack = createTrack(properties.SwingAnimations[rng:NextInteger(1, #properties.SwingAnimations)], animator)
	local viewmodelTrack = createTrack("rbxassetid://18628197508", camera.Knife.Humanoid.Animator)
	swingTrack:Play()
	viewmodelTrack:Play()
	viewmodelTrack:AdjustSpeed(1.5)
	swingTrack:AdjustSpeed(1/properties.AnimationDuration)
	swung:FireServer()
	task.wait(0.25)
	swingDebounce = false
end)

local swayCFrame = CFrame.new()

RunService.RenderStepped:Connect(function(dt)
	if player.Character.Humanoid.Health <= 0 then
		if camera:FindFirstChildWhichIsA("Model") ~= nil then
			camera.Knife:Destroy()
		end
	end
	
	if equipped then
		if camera:FindFirstChild("Knife") ~= nil then
			camera.Knife:SetPrimaryPartCFrame(camera.CFrame)
			
			local mouseDelta = UserInputService:GetMouseDelta()/50
			
			local swayX = math.clamp(mouseDelta.X, -0.10, 0.10)
			local swayY = math.clamp(mouseDelta.Y, -0.10, 0.10)
			
			swayCFrame = swayCFrame:Lerp(CFrame.new(swayX, swayY, 0), 5 * dt)
			
			if mouseDelta ~= 0 then
				camera.Knife:SetPrimaryPartCFrame(camera.CFrame * swayCFrame)
			end
		end
	end
end)
1 Like

It could probably be from setting the primarypart of the knife to the camera cframe, but im not too sure,

2 Likes

thats what im thinking too, but any other animation thats not related to equipping works fine, so im kinda lost there

2 Likes

Is it possible to send me a copy of it including the animations so I can help debug it.

2 Likes

yea sure,
knifeTest.rbxl (97.4 KB)

this is the place folder, has the local script in the knife tool inside of the startepack, and with the module script

1 Like

Honestly to me it looks normal, what exactly is wrong with it?

2 Likes

so the knife equip and knife unequip animations look like this


but when you hit play, its sort of trying to tween more than 1 thing at once

1 Like

Though when I try it with roblox animator, it looks how it supposed to look.

1 Like

have you tried it in testing mode yet?

i meant did you hit play yet and equipped the knife tool

1 Like

I have tried it in play test, though the only problem I see is the animation going back to the default position after the animation ends

1 Like

yeah thats what im trying to solve, is why its going to the default position after it ends

1 Like

Oh well in that case, I know the cause for it. First the equipping happens because it takes time for the knife to move to the starting position, though I don’t know how to like start from the animation position since im not an animator. Second the unequip happens because you’re waiting for the animation to end. Though it doesn’t fire exactly ends when the animation finishes, it fires after the animations ended and moves back to the default position. Though I know a fix for the unequip, you can use task.wait(animation.Length), or a keyframe event.

1 Like

The animations basically has a fade in and fade out. .Ended:Wait() includes in waiting fade out to finish.

2 Likes

thanks for the explanation! i never really knew that it has to go to the starting position, do you think making a holding idle animation would fix the issue, and set the actual position below the camera where the player cant see it, then play it after the equip animation?

1 Like

That could probably work.
[][][][][]

1 Like

after trying this out and moving the starting position of the CFrame, it had nothing to do to where i was setting it, but i did make a discovery. I noticed that if i didnt set the CFrame, the animation was still wonky, and by adjusting the weight, i was decreasing the time it took to get to the starting position. I thought that there was two animations playing at the same time, but grabbing that value only shows 1 animation playing at a time. so now im stuck again on what to do

1 Like

Well honestly though I can’t help much here, since I don’t know much with animations.

2 Likes

Its all good, i was researching a bit and i noticed that it does take a bit of time to travel to the first keyframe, and by using play it has a default time of 0.1 seconds. you can adjust that time aswell, so my hacky sollution was to set the parts invisible for the very very beginning, then play the animation and it looks like it works, im looking for a more concrete sollution though

1 Like

I’ve made some research and you can just do this.

local viewModelTrack = createTrack("rbxassetid://18629599562", camera.Knife.Humanoid.Animator)
	equipTrack:Play(0)
	viewModelTrack:Play(0)

You can set the fadetime to 0.

1 Like