Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a script act as a toggle?

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?

1 comment

0

If you post the script in your tool. People may be able to help you a tad bit more :)

2 answers

✓ Accepted answer

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)

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

1 comment

0

but why false? the variable name won't make sense if you set it to false when it is playing

This discussion is preserved as it was. New answers, comments, and votes are closed.