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

How to Play and Stop an animation with the same Key?

Right now I can Play the Animation with "c" and Stop the Animation with "x". But when I change "x" for "c", It does like both play and stop at the same time. How can I make it so that I can Play the animation with "c" and only if I played the animation can I then stop the animation with "c".

--LocalPlayer's Camera
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local character = player.Character

--Crouch Animation
local mouse = player:GetMouse()
local Animate 
local Humanoid = player.Character:FindFirstChild('Humanoid')
local CrouchAnimation = game.StarterPlayer.StarterPlayerScripts.Animate
		:WaitForChild("crouch"):FindFirstChild("CrouchAnim")

mouse.KeyDown:Connect(function(Key) 
	if Key == "c" then
		Animate = Humanoid:LoadAnimation(CrouchAnimation)
		Humanoid.CameraOffset = Vector3.new(0, -1.2, -1.5)
		Animate:Play()
		print("Crouched")
		mouse.KeyDown:Connect(function(Key)
			if Key == "x" then
				Humanoid.CameraOffset = Vector3.new(0, 0.2, -1.5)
				Animate:Stop()
				print("unCrouched")
			end
		end)
	end
end)

1 comment

0

Wait I just thought of how I can do this. If it works, I'll post the Answer!

1 answer

Alright I made a statement of the character being in crouched Position!

AlreadyCrouched = false

With this Statement I can check to see if the character is already in crouch or not!

--LocalPlayer's Camera
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local character = player.Character

--Crouch Animation
local mouse = player:GetMouse()
local Animate 
local Humanoid = player.Character:FindFirstChild('Humanoid')
local CrouchAnimation = game.StarterPlayer.StarterPlayerScripts.Animate
		:WaitForChild("crouch"):FindFirstChild("CrouchAnim")
local AlreadyCrouched = false

mouse.KeyDown:Connect(function(Key) 
	if Key == "c" and AlreadyCrouched == false then
			Animate = Humanoid:LoadAnimation(CrouchAnimation)
			Humanoid.CameraOffset = Vector3.new(0, -1.2, -1.5)
			Animate:Play()
			AlreadyCrouched = true
			print("Crouched")
	elseif Key == "c" and AlreadyCrouched == true then
			Humanoid.CameraOffset = Vector3.new(0, 0.2, -1.5)
			Animate:Stop()
			AlreadyCrouched = false
			print("unCrouched")
	end
end)

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