How to detect if an animation is playing or not

add these lines to every code that is playing an animation

1 Like

I tried again with this code, before I had put it wrong, the problem is that it lowers the stamina all the time, but the good thing is that it reduces 20 stamina by pressing the c, the problem is that if I keep pressed it reduces more than expected, I don’t know if that maybe can be done by adding a delay? I don’t know

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()```
1 Like

Crouch Code

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://18984972973"
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

is the crouching script is a local script ?

1 Like

if it is a local script then you have to change it into a script , but you can not use user input service in script so we are gonna use remote events

1 Like

if it keeps going down all the time then you have to increase the stamina deduct interval value

1 Like

Try this one

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
local staminaThreshold = 25

local function updateSpeed()
    -- Ensure stamina value is accessible and valid
    if StaminaSystem.StaminaValue and StaminaSystem.StaminaValue.Value <= 0 then
        humanoid.WalkSpeed = minimumSpeed
    elseif StaminaSystem.StaminaValue and StaminaSystem.StaminaValue.Value > staminaThreshold then
        humanoid.WalkSpeed = normalSpeed
    end
end

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

-- Start stamina deduction loop
coroutine.wrap(deductStamina)()
1 Like

it works, but when you add the attribute to the player, it does not take 20 stamina from him

1 Like

I added this code in a localscript in starterplayerscripts

local Players = game:GetService("Players")

local function onAgachadoChanged(player)
	local playerGui = player:WaitForChild("PlayerGui")
	local staminaFolder = playerGui:WaitForChild("Stamina")
	local staminaValue = staminaFolder:WaitForChild("StaminaValue")

	if player:GetAttribute("Agachado") then
		if not player:GetAttribute("AgachadoProcessed") then
			staminaValue.Value = staminaValue.Value - 20
			player:SetAttribute("AgachadoProcessed", true)
		end
	else
		player:SetAttribute("AgachadoProcessed", false)
	end
end

local function onPlayerAdded(player)
	player.AttributeChanged:Connect(function(attribute)
		if attribute == "Agachado" then
			onAgachadoChanged(player)
		end
	end)
end

Players.PlayerAdded:Connect(onPlayerAdded)

for _, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player)
end
2 Likes

now I have one more problem and that is that if the player doesn’t have 20 stamina, ideally the animation can’t be played, but that’s not happening, and the player can still make the animation and his stamina drops to negative directly

1 Like

so that the stamina does not go negative, I think we could add a limit at 0, in my stamina code which is this:

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
local staminaThreshold = 25

local function updateSpeed()
	-- Ensure stamina value is accessible and valid
	if StaminaSystem.StaminaValue and StaminaSystem.StaminaValue.Value <= 0 then
		humanoid.WalkSpeed = minimumSpeed
	elseif StaminaSystem.StaminaValue and StaminaSystem.StaminaValue.Value > staminaThreshold then
		humanoid.WalkSpeed = normalSpeed
	end
end

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

-- Start stamina deduction loop
coroutine.wrap(deductStamina)()```
to make it impossible to play the animation until the player has stamina, I don't know how to do it.
1 Like

hey iam back again it looks like that u r using a local script my friend , you are adding the attribute in the client only use a remote event to trigger when the player clicks the c button fire the event to the server and add the attribute there !

do you want to make the player can not crouch if he has much stamina ?

Hello! I’m sorry I didn’t answer but I could finally solve it, this is my final script in case someone in the future can use it:

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://YOURIDHERE"
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)

Thank you very much for all the answers, they were very helpful in solving my problem.

1 Like

do not mention it my friend (form form form – to accept the message)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.