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

Make a looped animation stop on another buttonpress?

Asked by 5 years ago

I understand this title is not very helpful as-is. I hope from what I've shown you can help understand what my error is and maybe help me out.

I have three buttons in a GUI which is given by a morph. Only one button is visible at a time, and the first two buttons play animations. These two are looped animations, and the final is a .1 second, non-looped, no movement animation in an attempt to cease the previous animation. The first two are idle-priority, and the final is action, so the priority has nothing to do with here.

Parent-Child Relationship

Each AnimID is correct and I've checked thrice over. First ID Second ID Third ID

Inside the LocalScript I set up what button activates what animation using this code:

wait(1)
local frame=script.Parent
local user=game.Players.LocalPlayer
repeat wait() until user.Character local char=user.Character
local humanoid=char:WaitForChild("Humanoid")
local anim
function playanim(id)
    if char~=nil and humanoid~=nil then
        local id="rbxassetid://"..tostring(id)
        local oldanim=char:FindFirstChild("LocalAnimation")
        if anim~=nil then
            anim:Stop()
        end
        if oldanim~=nil then
            if oldanim.AnimationId==id then
                oldanim:Destroy()
                return
            end
            oldanim:Destroy()
        end
        local animation=Instance.new("Animation",char)
        animation.Name="LocalAnimation"
        animation.AnimationId=id
        anim=humanoid:LoadAnimation(animation)
        anim:Play()
    end
end
local b1=frame.SP
b1.MouseButton1Down:connect(
function()
    playanim(b1.AnimID.Value)
end)
local b2=frame.SP2
b2.MouseButton1Down:connect(
function()
    playanim(b2.AnimID.Value)
    wait(3)
    anim:Stop()
end)
local b3=frame.BBK
b3.MouseButton1Down:connect(
function()
    playanim(b3.AnimID.Value)
end)
local b4=frame.BBK2
b4.MouseButton1Down:connect(
function()
    playanim(b4.AnimID.Value)
end)
local b5=frame.SP3
b5.MouseButton1Down:connect(
function()
    playanim(b5.AnimID.Value)
    anim:Stop()
end)
local b6=frame.BBK3
b6.MouseButton1Down:connect(
function()
    playanim(b6.AnimID.Value)
end)

Pay no mind to SP, the problem is with the BBK buttons. On button press one, it plays the first animation and executes the following script:

local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Character
local frame = script.Parent.Parent
local button = script.Parent
function onClick(mouse)
    wait(.5)
    player.Chest.Middle.A.BBKC.Enabled = true
    button.Visible = false
    frame.BBK2.Visible = true
end
script.Parent.MouseButton1Down:connect(onClick)

The second button is then pressed, executing this script:

local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Character
local frame = script.Parent.Parent
local button = script.Parent
function onClick(mouse)
    wait(.5)
    player.Chest.Middle.A.BBKC.Enabled = false
    player.Chest.Middle.B.BBK.Enabled = true
    button.Visible = false
    frame.BBK3.Visible = true
end
script.Parent.MouseButton1Down:connect(onClick)

Both of these scripts work fine, and the third script on button three is executed:

local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Character
local frame = script.Parent.Parent
local button = script.Parent
function onClick(mouse)
    wait(.5)
    player.Chest.Middle.B.BBK.Enabled = false
    button.Visible = false
    frame.BBK.Visible = true
end
script.Parent.MouseButton1Down:connect(onClick)

This script works fine, however the animation from button two will not stop. Can anyone help me make the animation stop on the third button press?

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

I am not sure why in your img you are using scripts in a gui? This should all be done in a local script as you are using player input.

Secondly I see no need to have a new script per buttion as they share a lot of the same functionality.

I would go for a more simple setup shown like

-- vars
local frm = script.Parent
local localPlr = game:GetService("Players").LocalPlayer
local plrChar = localPlr.Character or localPlr.CharacterAdded:Wait()
local humanoid = plrChar:WaitForChild("Humanoid")
local loadedAnimations = {}
local activeAnimation 

local function loadAnimation(btn, id)
    local animation = Instance.new("Animation")
    animation.AnimationId = "rbxassetid://" .. tostring(id)

    local animTrack = humanoid:LoadAnimation()

    loadedAnimations[btn] = function()
        if activeAnimation then
            activeAnimation:Stop()
        end
        activeAnimation = animTrack
        animTrack:Play()
    end

    return btn
end

-- create animation play function for each button
local b1 = loadAnimation(frm:WaitForChild("SP"), [some id])
local b2 = loadAnimation(frm:WaitForChild("SP2"), [some id])
local b3 = loadAnimation(frm:WaitForChild("BBK"), [some id])
local b4 = loadAnimation(frm:WaitForChild("BBK2"), [some id])
local b5 = loadAnimation(frm:WaitForChild("SP3"), [some id])
local b6 = floadAnimation(rm:WaitForChild("BBK3"), [some id])

-- setup each button as needed
b1.MouseButton1Click:Connect(function()
    loadedAnimations[b1]() -- run play function
end)
b2.MouseButton1Click:Connect(function()
    loadedAnimations[b2]() 

    -- hide any gui buttons / frames if needed

    wait(3)
    activeAnimation:Stop()
end)
b3.MouseButton1Click:Connect(function()
    loadedAnimations[b3]()
end)
b4.MouseButton1Click:Connect(function()
    loadedAnimations[b4]() 
end)
b5.MouseButton1Click:Connect(function()
    loadedAnimations[b5]() 
end)
b6.MouseButton1Click:Connect(function()
    loadedAnimations[b6]() 
end)

Your error may be down to how you have setup the scripts as you have added a lot of waits into your code.

Lastly I am not sure what player.Chest.Middle.A.BBKC.Enabled = false is but this change will only be local.

If your code feels too complicated it probably is and should be changed to something more suitable.

This is all I can really help with as I cannot replicate your setup. Hope this helps.

Ad

Answer this question