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

Use animation with a model in it from an animation GUI?

Asked by
hwlq 2
4 years ago

We are making an animation GUI for my game, and it features some animations that use models in them. The Model works in the animation editor, but we can't figure out how to spawn it in from a GUI button just for the animation, and then get rid of it after without it being bugged. We have managed to spawn it in but it is EXTREMELY buggy.

The model itself consists of 3 unions, and they are all welded together. A Motor6D is placed in the torso of our dummy, and part0 is welded to the HumanoidRoot, and part1 to a part within the model, which is also in the torso.

Here is the code for doing this

local animation = script.Animation1
local VectorZero = Vector3.new(0,0,0)
local localPlayer = script.Parent.Parent.Parent.Parent.Parent
local Character = localPlayer.Character
local Humanoid = Character:WaitForChild("Humanoid")
local hasSat = false
local CanEmote = true
local isAllowed = false
local isNoCollided = false
local isFalling = false
local hasBeenChecked = false
local isActive = false
local bypassed = require(game.ReplicatedStorage.drvkStuff)
local animation1
local chair
local allowedUsers = 
{
    "hwlq";
    "drvk";
}


local function stopAnimationsTracks()
    -- Get playing animations
    local AnimationTracks = Humanoid:GetPlayingAnimationTracks()

    -- Stop all playing animations
    for i, track in pairs (AnimationTracks) do
        track:Stop()
    end
end

local function setupChair()
    chair = game.ServerStorage.God.Chair:Clone()
    local motor6 = Instance.new("Motor6D")
    motor6.Part0 = Character.HumanoidRootPart
    motor6.Part1 = chair.Main 
    chair.Parent = Character.Torso
    chair.Main.CFrame = Character.Torso.CFrame
    chair.Union.CFrame = Character.Torso.CFrame
    chair.Union2.CFrame = Character.Torso.CFrame
    chair.Main.Parent = Character.Torso
    motor6.Parent = chair
    --chair.CFrame = Character.Torso.CFrame
end

local function userIsAllowed()
    if isAllowed then
        return
    end
    if not hasBeenChecked then
        for i, v in pairs (allowedUsers) do
            if localPlayer.Name == v then
                isAllowed = true
                return
            end
        end
        isAllowed = false
        hasBeenChecked = true
    end
end

local function makePlayerNoColide()
    Character.Torso.CanCollide = false
    Character.Head.CanCollide = false
end

local function makePlayerCollide()
    Character.Torso.CanCollide = true
    Character.Head.CanCollide = true
end

local function findAllContributorChairs()
    for i,v in pairs (Character.Torso:GetChildren()) do
        if v.Name == "Chair" then
            v:Destroy()
        end
    end
end

local function setupCharacter()
    if not Character then
        Character = localPlayer.CharacterAdded:Wait()
    end
    Humanoid:WaitForChild("Animator")
    animation1 = Humanoid:LoadAnimation(animation)
end
setupCharacter()

local function isAnimationNilAndPlaying()
    if animation1 and animation1.IsPlaying then
        animation1:Stop()
        isActive = false
    end
    if isAllowed then
        findAllContributorChairs()
    end
    if not Character.Torso.CanCollide and not Character.Head.CanCollide then
        makePlayerCollide()
    end
end

local function Moved()      
    if Humanoid.MoveDirection ~= VectorZero then
        isAnimationNilAndPlaying()
    end
end

local function onSeated(isSeated)
    if isSeated then
        hasSat = true
        if isActive then
            isAnimationNilAndPlaying()
            isActive = false
        end
    else 
        hasSat = false
    end
end

local function onJumping()
    isAnimationNilAndPlaying()
end

local function isFreeFalling(falling)
    if falling then
        isFalling = true
    else
        isFalling = false
    end
end


animation1:GetMarkerReachedSignal("Chair"):Connect(function()
    chair.Transparency = 0
end)

animation1.KeyframeReached:Connect(function(keyframeName)
    print("Working")
    local timeOfKeyFrame = animation1:GetTimeOfKeyframe("LoopStart")
    if keyframeName == "LoopEnd " then
        animation1.TimePosition = timeOfKeyFrame
        print("Working Reeee")
    end
end)

script.Parent.MouseButton1Click:Connect(function()
    userIsAllowed()
    if isAllowed and not isFalling and not isActive and CanEmote and not hasSat then
        CanEmote = false
        findAllContributorChairs()
        makePlayerNoColide()
        stopAnimationsTracks()
        isActive = true
        animation1.Priority = Enum.AnimationPriority.Action
        setupChair()
        animation1.Looped = true
        animation1:Play()
        wait(5)
        CanEmote = true
    end
end)


Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(Moved)
Humanoid.FreeFalling:Connect(isFreeFalling)
Humanoid.Seated:Connect(onSeated)
Humanoid.Jumping:Connect(onJumping)

I have no idea if there is a better way of doing this, but there are many problems with it as it is now. Does anyone have any idea how to help with this?

Answer this question