Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't my animations play in 2 things at once?

Asked by 6 years ago
local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled("Backpack", false)
--Services
local UIS = game:GetService("UserInputService")
local Replicated = game:GetService("ReplicatedStorage")
local RunServ = game:GetService("RunService")
--Hierarchy
local Assets = Replicated:WaitForChild("Assets")
    local Arms = Assets:WaitForChild("Arms")
    local Anims = Assets:WaitForChild("Animations")
    local Events = Assets:WaitForChild("Events")
    local Weapons = Assets:WaitForChild("Weapons")
    local Modules = Assets:WaitForChild("Modules")
        local WeaponData = require(Modules:FindFirstChild("WeaponData"))
--Variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local cam = workspace.CurrentCamera
local RawUnpack = unpack
local weaponAnimPlaying
--Tables
local toRemove = {"LeftArm", "RightArm", "LeftSleeve", "RightSleeve"}
local clientloadedAnimations, serverloadedAnimations = {}, {}
local weaponGuide = {"Primary", "Secondary", "Tactical"}

player.CameraMaxZoomDistance = 10
player.CameraMinZoomDistance = 10
--Functions
local function Weld(base, part) --(BasePart, Part)
    local w = Instance.new("Motor6D", base)
    w.C1 = part.CFrame:toObjectSpace(base.CFrame)
    w.Part0 = base
    w.Part1 = part  
    return w
end

local function EquipArms()
    if player.Character and player.Character:FindFirstChild("Humanoid") then
        for i,v in pairs(toRemove) do
            if player.Character:FindFirstChild(v) then
                player.Character[v].Transparency = 1
            end
        end
        local clonedArms = Arms.ViewModelArms:Clone()
        clonedArms.Name = "ViewModelArms"
        clonedArms.Parent = cam
        clonedArms.HumanoidRootPart.CFrame = player.Character.HumanoidRootPart.CFrame
        local w = Weld(player.Character.HumanoidRootPart, clonedArms.HumanoidRootPart)
        w.Name = "RootMotor"
    end
end

local function GetAnimation(anim, tbl)
    for i,v in pairs(tbl) do
        if i == anim then
            return v
        end
    end
end

local function PlayAnimation(anim)
    GetAnimation(anim, serverloadedAnimations):Play()
    GetAnimation(anim, clientloadedAnimations):Play()   
end

local function LoadAnimations(tbl)
    if player.Character and player.Character:FindFirstChild("Humanoid") and player.Character:FindFirstChild("Humanoid") ~= nil then
        for i,v in pairs(Anims:GetChildren()) do
            for x,d in pairs(v:GetChildren()) do
                if tbl == clientloadedAnimations then
                    clientloadedAnimations[d] = cam:WaitForChild("ViewModelArms"):WaitForChild("AnimationController"):LoadAnimation(d) --ViewModelArms
                elseif tbl == serverloadedAnimations then
                    serverloadedAnimations[d] = player.Character:WaitForChild("Humanoid"):LoadAnimation(d) --RealArms
                end
            end
        end
    end
end

local function EquipGun(target, data)
        if weaponAnimPlaying ~= nil then
            GetAnimation(weaponAnimPlaying, clientloadedAnimations):Stop()
            GetAnimation(weaponAnimPlaying, serverloadedAnimations):Stop()
        end
        for i,v in pairs(cam["ViewModelArms"]:GetChildren()) do
            if Weapons:FindFirstChild(v.Name) then
                v:Destroy()
            end
        end
        local gunName, gunSkin = player["Data"]["Equipped"].Value, player["Data"]["Equipped"]["Skin"].Value
        if gunName ~= nil and gunName ~= "" then
            local gunModel = Weapons:FindFirstChild(gunName)
            if gunModel and gunModel ~= nil then
                local clonedGun = gunModel:Clone()
                clonedGun.PrimaryPart.CFrame = cam["ViewModelArms"].RightArm.CFrame*CFrame.new(WeaponData.Hold[gunName][1])*WeaponData.Hold[gunName][2]
                Weld(cam["ViewModelArms"].RightArm, clonedGun.PrimaryPart)
                clonedGun.Parent = cam.ViewModelArms
                PlayAnimation(Anims[gunName].Hold) --this is where the animation is played at
                weaponAnimPlaying = Anims[gunName].Hold
            end
        end     
end

--Events
Events.EquipLoadout.OnClientEvent:connect(function(target, ...)
    EquipArms()
    clientloadedAnimations = {}
    LoadAnimations(clientloadedAnimations)--weaponAnimPlaying
    EquipGun(target, ...)
end)
--Other
repeat wait() until player and player.Character
LoadAnimations(serverloadedAnimations)

That code is in a localscript in StarterGui.

When I equip a gun I want a 'Hold' animation to play to a set of ViewModelArms (has an AnimationController in it) and the player's character's Humanoid. When I run the script and print some things, it says the animation in fact IS playing in both the Humanoid and AnimationController, yet I only see it visually playing in 1 of them. How is this come and how can I make it play to both? Thanks for help!

Answer this question