Hello developer formus!
Im having an issues determining what I should use to play the animations of my viewmodel. My goal is to have it so when I start playing a new animation, the old one stops. I have no clue why but using the :Stop() on the animation isnt working and the animations end up playing over each other and making everything appear slow and not as life like.
The script:
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character
character.Archivable = true
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local characterLoaded = false
local loaded = player.CharacterAppearanceLoaded:Connect(function()
characterLoaded = true
end)
local inventory = {
"Assault Rifle";
"Pistol";
}
repeat wait() until characterLoaded == true
for _,part in character:GetDescendants() do
if part:IsA("Part") then
part.CastShadow = false
end
if part:IsA("Accessory") or part:IsA("Script") then
part.Archivable = false
end
if part.Name == "Right Hip" or part.Name == "Left Hip" then
part.Archivable = false
end
end
local currentGun = nil
local viewmodel = character:Clone()
viewmodel.Parent = camera
viewmodel.Name = "Viewmodel"
viewmodel.Head.Anchored = true
local torso = viewmodel.Torso
local leftLeg = viewmodel["Left Leg"]
local rightLeg = viewmodel["Right Leg"]
local rightArm = viewmodel["Right Arm"]
local gunBone = nil
local idleAnimation = nil
local fireAnimation = nil
local aimAnimation = nil
local reloadAnimation = nil
function loadGun(gun, typeOfGun)
local gunFolder = game.ReplicatedStorage.Guns
currentGun = gunFolder:FindFirstChild(gun):Clone()
currentGun.Parent = viewmodel
-- Create Bone
gunBone = Instance.new("Motor6D")
gunBone.Name = "Handle"
gunBone.Parent = rightArm
gunBone.Part0 = viewmodel["Right Arm"]
gunBone.Part1 = currentGun.Handle
-- Animation
idleAnimation = currentGun:FindFirstChild("Idle")
idleAnimation.Parent = viewmodel.Humanoid
fireAnimation = currentGun:FindFirstChild("Fire")
fireAnimation.Parent = viewmodel.Humanoid
aimAnimation = currentGun:FindFirstChild("Aim")
aimAnimation.Parent = viewmodel.Humanoid
reloadAnimation = currentGun:FindFirstChild("Reload")
reloadAnimation.Parent = viewmodel.Humanoid
end
function updatePositions()
leftLeg.CFrame = (character["Left Leg"].CFrame - character.Head.CFrame.Position) + viewmodel.Head.CFrame.Position
leftLeg.CFrame = leftLeg.CFrame:ToWorldSpace(CFrame.new(0,0,0))
rightLeg.CFrame = (character["Right Leg"].CFrame - character.Head.CFrame.Position) + viewmodel.Head.CFrame.Position
rightLeg.CFrame = rightLeg.CFrame:ToWorldSpace(CFrame.new(0,0,0))
end
local viewmodelPosition = viewmodel.Head
local lastCF = CFrame.new(0,0,0)
local swayCF = CFrame.new()
local currentSwayAMT = .6
local lastCameraCF = CFrame.new()
local target = viewmodelPosition
local reloadTimer = 0
local isAiming = false
local currentAnimation = nil
loadGun("Assault Rifle") -- Gun to spawn with
viewmodel.Humanoid.Animator:LoadAnimation(idleAnimation):Play()
RunService.RenderStepped:Connect(function(delta)
if isAiming == true then
target = currentGun.Aim
else
target = viewmodelPosition
end
local rot = camera.CFrame:ToObjectSpace(lastCameraCF)
local X, Y, Z = rot:ToOrientation()
lastCameraCF = camera.CFrame
-- Main Loop
if characterLoaded == true then
swayCF = swayCF:Lerp(CFrame.Angles(math.sin(X) * currentSwayAMT, math.sin(Y) * currentSwayAMT, 0), .1)
target:PivotTo(camera.CFrame * swayCF)
updatePositions()
--updateGun()
if reloadTimer ~= 0 then
reloadTimer -= delta
if reloadTimer <= 0 then
reloadTimer = 0
end
end
end
end)
function changeGun(amount)
currentAnimation = currentGun:FindFirstChild("Idle")
--idleAnimation.Parent = viewmodel.Humanoid
end
function playAnimation(name) -- Idle, Fire, Aim, Reload
end
UserInputService.InputBegan:Connect(function(input)
if reloadTimer == 0 then
if input.UserInputType == Enum.UserInputType.MouseButton2 then
print("Right Click")
if isAiming == true then
isAiming = false
--viewmodel.Humanoid.Animator:LoadAnimation(aimAnimation):Stop()
viewmodel.Humanoid.Animator:LoadAnimation(idleAnimation):Play()
else
isAiming = true
viewmodel.Humanoid.Animator:LoadAnimation(idleAnimation):Stop()
--viewmodel.Humanoid.Animator:LoadAnimation(aimAnimation):Play()
end
end
if input.KeyCode == Enum.KeyCode.R then
reloadTimer = 2
print("R")
viewmodel.Humanoid.Animator:LoadAnimation(idleAnimation):Stop()
viewmodel.Humanoid.Animator:LoadAnimation(aimAnimation):Stop()
viewmodel.Humanoid.Animator:LoadAnimation(reloadAnimation):Play()
end
end
end)
And here is the game file:
Framework.rbxl (105.4 KB)