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

How do I make a script act as a toggle?

Asked by 6 years ago

I made a weapon an I created an aiming animatin. I scripted everything and now if I press right click it plays the aiming animation. How would I make it to where if you are already aiming, and if you press mouse button 2 again it will stop? Does anybody know how to do this?

0
If you post the script in your tool. People may be able to help you a tad bit more :) GottaHaveAFunTime 218 — 6y

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

You can "pause" your animation using the AdjustSpeed function on the AnimationTrack object. Wait until the button has been clicked again, and unpause :)

local plr = game.Players.LocalPlayer; --Player
local char = plr.Character or plr.CharacterAdded:Wait(); --Character
local human = char:WaitForChild("Humanoid"); --Humanoid
local m = plr:GetMouse(); --Mouse
local aiming = false; --Aiming variable

local anim = Instance.new("Animation");
anim.AnimationId = "rbxassetid://" --Your id

local stopAt = .3; --The `TimePosition` in which the anim should stop at

m.Button2Down:Connect(function()
    if not aiming then
        aiming = true;
        local track = human:LoadAnimation(anim);
        track:Play(); --Play it
        repeat wait() until track.TimePosition >= stopAt
        track:AdjustSpeed(0); --Pause it
        m.Button2Down:Wait(); --Wait until clicked again
        track:Stop(); --Stop it
        wait();
        aiming = false;
    end
end)
Ad
Log in to vote
0
Answered by
Viking359 161
6 years ago

Do something like this with a bool

local isplaying = false
if isplaying == false then
--animation stuff here
isplaying = true
--change it to false here if you want
elseif isplaying == true then
--[[animation]]:Stop()
--anything else here
0
but why false? the variable name won't make sense if you set it to false when it is playing hiimgoodpack 2009 — 6y

Answer this question