How to detect if an animation is playing or not

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local StaminaSystem = require(player:WaitForChild("PlayerGui"):WaitForChild("Stamina"):WaitForChild("StaminaModule"))

local staminaDeductRate = 1
local staminaDeductInterval = 0.1
local minimumSpeed = 16
local normalSpeed = 49  -- MaxSpeed
local staminaThreshold = 25
local animationCost = 20

-- Set this attribute name according to your needs
local attributeName = "Agachado"

local function updateSpeed()
	if StaminaSystem.StaminaValue.Value <= 0 then
		humanoid.WalkSpeed = minimumSpeed
	elseif StaminaSystem.StaminaValue.Value > staminaThreshold then
		humanoid.WalkSpeed = normalSpeed
	end
end

local function isAnimationPlaying()
	return player:GetAttribute(attributeName) == true
end

local function handleStaminaDuringAnimation()
	if isAnimationPlaying() then
		if StaminaSystem.StaminaValue.Value >= animationCost then
			StaminaSystem.RemoveStamina(animationCost)
		else
			-- Detener la animación si no hay suficiente stamina
			player:SetAttribute(attributeName, nil)
		end
	end
end

local function deductStamina()
	while humanoid:GetState() ~= Enum.HumanoidStateType.Dead do
		if (humanoid.WalkSpeed > minimumSpeed and StaminaSystem.StaminaValue.Value > 0) or isAnimationPlaying() then
			StaminaSystem.RemoveStamina(staminaDeductRate)
			StaminaSystem.StaminaCanRegen = false
		elseif humanoid.WalkSpeed <= minimumSpeed then
			StaminaSystem.StaminaCanRegen = true
		end

		handleStaminaDuringAnimation()
		updateSpeed()
		wait(staminaDeductInterval)
	end
end

-- Inicia la deducción de stamina cuando el script se ejecuta
deductStamina()

-- Manejo de la tecla 'C' para activar la animación
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed and input.KeyCode == Enum.KeyCode.C then
		-- Activa el atributo para realizar la animación
		player:SetAttribute(attributeName, true)
		-- Aquí puedes iniciar la animación si lo deseas

		-- Luego, para desactivar el atributo (cuando dejes de hacer la animación), puedes hacerlo en otra parte del código
	end
end)

this is the code, I don’t know if I’m missing something, I just don’t know much about coding

1 Like

here local function isAnimationPlaying() return player:GetAttribute(attributeName) == true end

change it to be local function isAnimationPlaying() return player:GetAttribute(attributeName) end

1 Like

do not worry i am gonna help you until you reach your needs

2 Likes

keeps going down even if I don’t make anything

1 Like
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local StaminaSystem = require(player:WaitForChild("PlayerGui"):WaitForChild("Stamina"):WaitForChild("StaminaModule"))

local staminaDeductRate = 1
local staminaDeductInterval = 0.1
local minimumSpeed = 16
local normalSpeed = 49  -- MaxSpeed
local staminaThreshold = 25
local animationCost = 20

-- Set this attribute name according to your needs
local attributeName = "Agachado"

local function updateSpeed()
	if StaminaSystem.StaminaValue.Value <= 0 then
		humanoid.WalkSpeed = minimumSpeed
	elseif StaminaSystem.StaminaValue.Value > staminaThreshold then
		humanoid.WalkSpeed = normalSpeed
	end
end

local function isAnimationPlaying()
	return player:GetAttribute(attributeName)
end

local function handleStaminaDuringAnimation()
	if isAnimationPlaying() then
		if StaminaSystem.StaminaValue.Value >= animationCost then
			StaminaSystem.RemoveStamina(animationCost)
		else
			-- Detener la animación si no hay suficiente stamina
			player:SetAttribute(attributeName, nil)
		end
	end
end

local function deductStamina()
	while humanoid:GetState() ~= Enum.HumanoidStateType.Dead do
		if (humanoid.WalkSpeed > minimumSpeed and StaminaSystem.StaminaValue.Value > 0) or isAnimationPlaying() then
			StaminaSystem.RemoveStamina(staminaDeductRate)
			StaminaSystem.StaminaCanRegen = false
		elseif humanoid.WalkSpeed <= minimumSpeed then
			StaminaSystem.StaminaCanRegen = true
		end

		handleStaminaDuringAnimation()
		updateSpeed()
		wait(staminaDeductInterval)
	end
end

-- Inicia la deducción de stamina cuando el script se ejecuta
deductStamina()

-- Manejo de la tecla 'C' para activar la animación
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed and input.KeyCode == Enum.KeyCode.C then
		-- Activa el atributo para realizar la animación
		player:SetAttribute(attributeName, true)
		-- Aquí puedes iniciar la animación si lo deseas

		-- Luego, para desactivar el atributo (cuando dejes de hacer la animación), puedes hacerlo en otra parte del código
	end
end)

1 Like

You could use print to check if the animation:play()

2 Likes

iam back again ! can you show me your crouching script ?

1 Like

make sure to remove the attribute after the players stands by setting the attribute to nil

1 Like

the code to crouch:


--Player--
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character.Humanoid
local Mouse = Player:GetMouse()
local Camera = game.Workspace.Camera
local Storage = ReplicatedStorage.Storage
local RunService = game:GetService("RunService")
local runs = Character:WaitForChild("Running")
--Crouch--
local CrouchAnimBackup = "rbxassetid://12474554475"
local SoundsFolder = Storage.Sounds
local CrouchSounds = SoundsFolder.Crouch
local GetUpSound = SoundsFolder.GetUp
local AnimationFolder = Storage.Animations
local CrouchAnimation = Humanoid:LoadAnimation(AnimationFolder.Crouch)


local function IsPlayerMoving() 
	if Humanoid.MoveDirection.Magnitude == 0 then
	end
end

RunService.RenderStepped:Connect(IsPlayerMoving)

--Main Script--
Mouse.KeyDown:Connect(function(Key)
	if Key == "c" then
		CrouchAnimation:Play()
		Humanoid.WalkSpeed = Humanoid.WalkSpeed/2
		Camera.CameraSubject = Character.Head;
		Camera.FieldOfView = 65
		local function IsPlayerMoving() 
			if Humanoid.MoveDirection.Magnitude == 0 then
				CrouchAnimation:AdjustSpeed(0)
			else
				CrouchAnimation:AdjustSpeed(1)
			end
		end
		CrouchSounds:Play()
		RunService.RenderStepped:Connect(IsPlayerMoving)
		Player.CameraMinZoomDistance = 7
	end   
end)

Mouse.KeyUp:Connect(function(Key)
	if Key == "c" then
		CrouchAnimation:Stop()
		Humanoid.WalkSpeed = Humanoid.WalkSpeed * 2
		Camera.FieldOfView = 70
		Camera.CameraSubject = Character.Humanoid
		Player.CameraMinZoomDistance = 0.5
		GetUpSound:Play()
	end
end)

thank you very much for being attentive and helpful, hopefully we can solve it

1 Like

i think i got it !

try these codes , stamina code :-
`local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild(“Humanoid”)
local StaminaSystem = require(player:WaitForChild(“PlayerGui”):WaitForChild(“Stamina”):WaitForChild(“StaminaModule”))

local staminaDeductRate = 1
local staminaDeductInterval = 0.1
local minimumSpeed = 16
local normalSpeed = 49 – MaxSpeed
local staminaThreshold = 25
local animationCost = 20

– Set this attribute name according to your needs
local attributeName = “Agachado”

local function updateSpeed()
if StaminaSystem.StaminaValue.Value <= 0 then
humanoid.WalkSpeed = minimumSpeed
elseif StaminaSystem.StaminaValue.Value > staminaThreshold then
humanoid.WalkSpeed = normalSpeed
end
end

local function handleStaminaDuringAnimation()
if player:GetAttribute(attributeName) == true then
if StaminaSystem.StaminaValue.Value >= animationCost then
StaminaSystem.RemoveStamina(animationCost)
else
– Detener la animación si no hay suficiente stamina
player:SetAttribute(attributeName, nil)
end
end
end

local function deductStamina()
while humanoid:GetState() ~= Enum.HumanoidStateType.Dead do
if (humanoid.WalkSpeed > minimumSpeed and StaminaSystem.StaminaValue.Value > 0) or player:GetAttribute(attributeName) == true then
StaminaSystem.RemoveStamina(staminaDeductRate)
StaminaSystem.StaminaCanRegen = false
elseif humanoid.WalkSpeed <= minimumSpeed then
StaminaSystem.StaminaCanRegen = true
end

	handleStaminaDuringAnimation()
	updateSpeed()
	wait(staminaDeductInterval)
end

end

– Inicia la deducción de stamina cuando el script se ejecuta
deductStamina()`

crouching code :-
`–Player–
local Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Player = Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character.Humanoid
local Mouse = Player:GetMouse()
local Camera = game.Workspace.Camera
local Storage = ReplicatedStorage.Storage
local RunService = game:GetService(“RunService”)
local runs = Character:WaitForChild(“Running”)
–Crouch–
local CrouchAnimBackup = “rbxassetid://12474554475”
local SoundsFolder = Storage.Sounds
local CrouchSounds = SoundsFolder.Crouch
local GetUpSound = SoundsFolder.GetUp
local AnimationFolder = Storage.Animations
local CrouchAnimation = Humanoid:LoadAnimation(AnimationFolder.Crouch)

local AttributeName = “Agachado”

local function IsPlayerMoving()
if Humanoid.MoveDirection.Magnitude == 0 then
end
end

RunService.RenderStepped:Connect(IsPlayerMoving)

–Main Script–
Mouse.KeyDown:Connect(function(Key)
if Key == “c” then
Player:SetAttribute(AttributeName , true)
CrouchAnimation:Play()
Humanoid.WalkSpeed = Humanoid.WalkSpeed/2
Camera.CameraSubject = Character.Head;
Camera.FieldOfView = 65
local function IsPlayerMoving()
if Humanoid.MoveDirection.Magnitude == 0 then
CrouchAnimation:AdjustSpeed(0)
else
CrouchAnimation:AdjustSpeed(1)
end
end
CrouchSounds:Play()
RunService.RenderStepped:Connect(IsPlayerMoving)
Player.CameraMinZoomDistance = 7
end
end)

Mouse.KeyUp:Connect(function(Key)
if Key == “c” then
Player:SetAttribute(AttributeName , nil)
CrouchAnimation:Stop()
Humanoid.WalkSpeed = Humanoid.WalkSpeed * 2
Camera.FieldOfView = 70
Camera.CameraSubject = Character.Humanoid
Player.CameraMinZoomDistance = 0.5
GetUpSound:Play()
end
end)`

1 Like

you can not use the user input service in regular scripts only in module scripts and local scripts

1 Like

it still doesn’t work, now the stamina doesn’t go down on its own but it never goes down, neither when I run nor when I do any of the certain animations

1 Like

at least it is doesnot go down on it is own again
you have to add the attribute when you play any animation

1 Like

this is very weird , why do you remove the attribute here :-

local function handleStaminaDuringAnimation()
	if player:GetAttribute(attributeName) == true then
		if StaminaSystem.StaminaValue.Value >= animationCost then
			StaminaSystem.RemoveStamina(animationCost)
		else
			-- Detener la animación si no hay suficiente stamina
			player:SetAttribute(attributeName, nil)
		end
	end
end
1 Like

how could i do the attribute inside the code? i tried modifying the intvalue myself and when i set it to a value lower than 100, it kept regenerating all the time. i guess it didn’t detect the movements because the attribute is not present, right?

1 Like

to remove the attribute use this line :-

player:SetAttribute(attributeName, nil)

and to add it use this one :-

Player:SetAttribute(AttributeName , true)

the attribute name have to be “Agachado”

1 Like

to check if the attribute has presented or not you will find it as a property in the player object (that is in the Players)

1 Like

should i add a code outside of this one to add the attribute when making the animation? or is it already incorporated with this code?

1 Like

the crouching code only will work

1 Like