You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want my player’s stamina to be reduced when making certain animations.
-
What is the issue? Include screenshots / videos if possible!
I don’t know how to implement it, I have a code to reduce it when running but I don’t know how to implement it inside my code so it also reduces for example 20 stamina points when doing any of the animations I have prepared.
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
yes, but I didn’t know how to put it in my code.
my code to reduce stamina:
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 function updateSpeed()
if StaminaSystem.StaminaValue.Value <= 0 then
humanoid.WalkSpeed = minimumSpeed
elseif StaminaSystem.StaminaValue.Value > staminaThreshold then
humanoid.WalkSpeed = normalSpeed
end
end
local function deductStamina()
while humanoid:GetState() ~= Enum.HumanoidStateType.Dead do
if humanoid.WalkSpeed > minimumSpeed 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
deductStamina()
I’m just getting started with this, and I don’t know much about it, if you have any questions feel free to ask, thank you very much for reading and I hope you can help me :D.
2 Likes
you can detect if the animation is playing or not by using AnimationTrack.IsPlaying
How could it be implemented in my code, I mean, what would my code look like in the end, and could it be possible to do it with multiple animations?
2 Likes
do you want to play multiple animation or you wanna detect if any of these animations is playing ?
I want to make it so that if the player does any of these animations, the stamina is reduced
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 -- MaxSpeed
local staminaThreshold = 25
local Animations = {Animation1 = humanoid:LoadAnimation(123456789)} -- put your animations here in the same way.
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 deductStamina()
while humanoid:GetState() ~= Enum.HumanoidStateType.Dead do
local IsAnimationPlaying = false
for i , animation in pairs(Animations) do
if animation.IsPlaying == true then
IsRunning = true
break
end
end
if (humanoid.WalkSpeed > minimumSpeed and StaminaSystem.StaminaValue.Value > 0) or IsAnimationPlaying then
StaminaSystem.RemoveStamina(staminaDeductRate)
StaminaSystem.StaminaCanRegen = false
updateSpeed()
elseif humanoid.WalkSpeed <= minimumSpeed then
StaminaSystem.StaminaCanRegen = true
end
wait(staminaDeductInterval)
end
end
deductStamina()
add your animations in the animations table in the same way
local Animations = {Animation1 = humanoid:LoadAnimation(18985720979)}{Animation1 = humanoid:LoadAnimation(123456)} -- put your animations here in the same way.
like that? sorry im new with this
1 Like
Unable to cast value to Object - Cliente - Stamina:11
1 Like
the idea would be to decrease the stamina by doing these animations, or else running, which I had already implemented in my code but now it doesn’t work
1 Like
no it is like this {animation1 = humanoid:LoadAnimation(Put the animation instance here) , animation2 = humanoid:LoadAnimation(Put the animation instance here) } etc…
sorry for this just replace these numbers with an animation instane like lets say that you have an animation instance in the workspace you have to replace these numbers with workspace.your-animation-name
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 ReplicatedStorage = game:GetService("ReplicatedStorage")
local staminaDeductRate = 1
local staminaDeductInterval = 0.1
local minimumSpeed = 16
local normalSpeed = 49 -- MaxSpeed
local staminaThreshold = 25
local Animations = {Animation1 = humanoid:LoadAnimation(ReplicatedStorage.Storage.Animations.Crouch)} -- put your animations here in the same way.
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 deductStamina()
while humanoid:GetState() ~= Enum.HumanoidStateType.Dead do
local IsAnimationPlaying = false
for i , animation in pairs(Animations) do
if animation.IsPlaying == true then
IsRunning = true
break
end
end
if (humanoid.WalkSpeed > minimumSpeed and StaminaSystem.StaminaValue.Value > 0) or IsAnimationPlaying then
StaminaSystem.RemoveStamina(staminaDeductRate)
StaminaSystem.StaminaCanRegen = false
updateSpeed()
elseif humanoid.WalkSpeed <= minimumSpeed then
StaminaSystem.StaminaCanRegen = true
end
wait(staminaDeductInterval)
end
end
deductStamina()
tried a single one but it still doesn’t work, the output says nothing.
1 Like
if you want I can send you a video of how it works
1 Like
btw, the script is located in starterCharacterScripts and is a localscript.
1 Like
you have to play and load the animations in the same script
make you can add an attribute to the player in a script *
and call it like “crouching” then detect if the player have this attribute or not
1 Like
how can I do that? for example the id I put there is the crouching animation, it is made using the ‘C’ key.
1 Like
easy enough !
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 – MaxSpeed
local staminaThreshold = 25
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 deductStamina()
while humanoid:GetState() ~= Enum.HumanoidStateType.Dead do
if (humanoid.WalkSpeed > minimumSpeed and StaminaSystem.StaminaValue.Value > 0) or player:GetAttribute(“Your Attribute name here”) then
StaminaSystem.RemoveStamina(staminaDeductRate)
StaminaSystem.StaminaCanRegen = false
updateSpeed()
elseif humanoid.WalkSpeed <= minimumSpeed then
StaminaSystem.StaminaCanRegen = true
end
wait(staminaDeductInterval)
end
end
deductStamina()`
and to attribute the player use Player:SetAttribute(Your attribute name here , true)
and to remove it use Player:SetAttribute(Your attribute name here , nil)
1 Like
it takes my stamina out without making anything, meaning it starts to go down even if I’m not doing anything.
1 Like