local StaminaManager = require(script.Parent.StaminaModule)
local defaultSpeed = 16
local sprintSpeed = 22
local normalFov = 70
local sprintFov = 80
local staminaDrainRate = 20
local staminaFillRate = 30
local staminaRegenDelay = 0.5
local crouchSpeed = 8
local normalSpeed = 16
local crouchHeight = 1.5
local normalHeight = 2
local stamina = 100
local maxStamina = 100
local minStamina = 0
-- [ Services ] --
local Services = setmetatable({}, {__index = function(Self, Index)
local NewService = game:GetService(Index)
if NewService then
Self[Index] = NewService
end
return NewService
end})
local UserInputService = Services.UserInputService
local TweenService = Services.TweenService
local RunService = Services.RunService
local Players = Services.Players
local ReplicatedStorage = Services.ReplicatedStorage
-- // Variables \ --
local variables = script.Parent.Variables
local sprintKey = Enum.KeyCode.LeftShift
local crouchKey = Enum.KeyCode.C
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Camera = workspace.CurrentCamera
local sprintAnim = ReplicatedStorage:WaitForChild("Animations"):WaitForChild("Player"):WaitForChild("SprintAnim")
local animTrack = Humanoid:LoadAnimation(sprintAnim)
--local crouchIdleAnimation = Humanoid:LoadAnimation(script.CrouchIdle)
--local crouchWalkAnimation = Humanoid:LoadAnimation(script.CrouchWalk)
local healthBar = game.Players.LocalPlayer.PlayerGui:WaitForChild("StaminaAndHealthBar").Frame.HealthBar.Bar
local staminaBar = game.Players.LocalPlayer.PlayerGui:WaitForChild("StaminaAndHealthBar").Frame.StaminaBar.Bar
local shiftPressed = false
local movementKeysPressed = {}
local staminaDepleted = false
local sprinting = variables.Sprinting.Value
local isCrouching = variables.Crouching.Value
local isMoving = false
local lastSprintTime = tick()
local movementKeys = {Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D}
-- // Functions \ --
local function tweenFov(duration, fov)
local tween = TweenService:Create(Camera, TweenInfo.new(duration, Enum.EasingStyle.Quad), { FieldOfView = fov })
tween:Play()
tween.Completed:Wait()
tween:Destroy()
end
local function tweenStaminaBar()
local tween = TweenService:Create(staminaBar, TweenInfo.new(0.25, Enum.EasingStyle.Quad), { Size = UDim2.new(stamina / maxStamina, 0, 1, 0) })
tween:Play()
end
local function startSprinting()
if not sprinting and stamina > minStamina and not isCrouching then
sprinting = true
Humanoid.WalkSpeed = sprintSpeed
animTrack:Play()
tweenFov(0.25, sprintFov)
end
end
local function stopSprinting()
if sprinting then
sprinting = false
Humanoid.WalkSpeed = defaultSpeed
animTrack:Stop()
tweenFov(0.25, normalFov)
end
end
local function crouch()
if not isCrouching and not sprinting then
isCrouching = true
Humanoid.WalkSpeed = crouchSpeed
HumanoidRootPart.Size = Vector3.new(HumanoidRootPart.Size.X, crouchHeight, HumanoidRootPart.Size.Z)
if isMoving then
--crouchIdleAnimation:Stop()
--crouchWalkAnimation:Play()
else
--crouchWalkAnimation:Stop()
--crouchIdleAnimation:Play()
end
end
end
Assistant
User
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid") :: Humanoid
local Animations = game:GetService("ReplicatedStorage"):WaitForChild("Animations"):WaitForChild("Player")
Assistant
local function stand()
if isCrouching then
isCrouching = false
Humanoid.WalkSpeed = normalSpeed
HumanoidRootPart.Size = Vector3.new(HumanoidRootPart.Size.X, normalHeight, HumanoidRootPart.Size.Z)
--crouchIdleAnimation:Stop()
--crouchWalkAnimation:Stop()
end
end
UserInputService.InputBegan:Connect(function(input, processed)
if processed then return end
if input.KeyCode == sprintKey then
if not staminaDepleted then
shiftPressed = true
if next(movementKeysPressed) ~= nil then
startSprinting()
end
end
elseif table.find(movementKeys, input.KeyCode) then
movementKeysPressed[input.KeyCode] = true
if shiftPressed and not staminaDepleted and not isCrouching then
startSprinting()
end
elseif input.KeyCode == crouchKey and not isCrouching and not sprinting then
crouch()
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == sprintKey then
shiftPressed = false
stopSprinting()
if not UserInputService:IsKeyDown(sprintKey) then
staminaDepleted = false
end
elseif table.find(movementKeys, input.KeyCode) then
movementKeysPressed[input.KeyCode] = nil
if next(movementKeysPressed) == nil then
stopSprinting()
end
elseif input.KeyCode == crouchKey then
stand()
end
end)
RunService.RenderStepped:Connect(function(deltaTime)
if sprinting then
stamina = math.max(minStamina, stamina - staminaDrainRate * deltaTime)
if stamina <= minStamina then
staminaDepleted = true
stopSprinting()
end
lastSprintTime = tick()
else
if tick() - lastSprintTime >= staminaRegenDelay then
stamina = math.min(maxStamina, stamina + staminaFillRate * deltaTime)
end
end
tweenStaminaBar()
end)
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if Humanoid.MoveDirection.Magnitude > 0 then
isMoving = true
if isCrouching then
--crouchIdleAnimation:Stop()
--if not crouchWalkAnimation.IsPlaying then
-- crouchWalkAnimation:Play()
--end
end
else
isMoving = false
if isCrouching then
--crouchWalkAnimation:Stop()
--if not crouchIdleAnimation.IsPlaying then
-- crouchIdleAnimation:Play()
--end
end
end
end)
local anim = game.ReplicatedStorage.Animations.Player.Land
local playAnim = script.Parent.Parent.Humanoid:LoadAnimation(anim)
local isJumping = false
Humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if animTrack.IsPlaying then
animTrack:Stop()
end
elseif newState == Enum.HumanoidStateType.Landed then
if animTrack.IsPlaying then
animTrack:Stop()
end
playAnim:Play()
playAnim.Stopped:Wait()
if sprinting then
animTrack:Play()
end
elseif newState == Enum.HumanoidStateType.Freefall then
if animTrack.IsPlaying then
animTrack:Stop()
end
end
end)
this is the StaminaManager module:
local StaminaManager = {}
local RunService = game:GetService("RunService")
StaminaManager.stamina = 100
local maxStamina = 100
local minStamina = 0
local lastStaminaUseTime = 0
local staminaRegenDelay = 0.5
local staminaRegenRate = 30
function StaminaManager.GetStamina()
return StaminaManager.stamina
end
function StaminaManager.AddStamina(amount)
local currentTime = tick()
if currentTime - lastStaminaUseTime >= staminaRegenDelay then
StaminaManager.stamina = math.min(maxStamina, StaminaManager.stamina + amount)
end
end
function StaminaManager.ReduceStamina(amount)
if StaminaManager.stamina >= amount then
StaminaManager.stamina = math.max(minStamina, StaminaManager.stamina - amount)
lastStaminaUseTime = tick()
return true
end
return false
end
local function autoRegenerateStamina()
RunService.Heartbeat:Connect(function(deltaTime)
StaminaManager.AddStamina(staminaRegenRate * deltaTime)
end)
end
autoRegenerateStamina()
task.spawn(function()
while true do
print(StaminaManager.stamina)
task.wait(.5)
end
end)
return StaminaManager
update the localscript to use the module. give me the whole code.
To update the LocalScript to utilize the StaminaManager module, we need to adjust the points where stamina is handled directly and replace those operations with calls to the module's functions. Here's the updated LocalScript incorporating methods from the StaminaManager:
-- // Constants \\ --
local StaminaManager = require(script.Parent.StaminaModule)
local defaultSpeed = 16
local sprintSpeed = 22
local normalFov = 70
local sprintFov = 80
local crouchSpeed = 8
local normalSpeed = 16
local crouchHeight = 1.5
local normalHeight = 2
local staminaDrainRate = 20
local staminaFillRate = 30 -- Not used directly anymore, handled by StaminaManager
local staminaRegenDelay = 0.5 -- Not used directly anymore, handled by StaminaManager
local maxStamina = 100 -- Not used directly anymore, handled by StaminaManager
-- [ Services ] --
local Services = setmetatable({}, {__index = function(Self, Index)
local NewService = game:GetService(Index)
if NewService then
Self[Index] = NewService
end
return NewService
end})
local UserInputService = Services.UserInputService
local TweenService = Services.TweenService
local RunService = Services.RunService
local Players = Services.Players
local ReplicatedStorage = Services.ReplicatedStorage
-- // Variables \\ --
local sprintKey = Enum.KeyCode.LeftShift
local crouchKey = Enum.KeyCode.C
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Camera = workspace.CurrentCamera
local sprintAnim = ReplicatedStorage:WaitForChild("Animations"):WaitForChild("Player"):WaitForChild("SprintAnim")
local animTrack = Humanoid:LoadAnimation(sprintAnim) -- Sprint animation
local healthBar = game.Players.LocalPlayer.PlayerGui:WaitForChild("StaminaAndHealthBar").Frame.HealthBar.Bar
local staminaBar = game.Players.LocalPlayer.PlayerGui:WaitForChild("StaminaAndHealthBar").Frame.StaminaBar.Bar
local shiftPressed = false
local movementKeysPressed = {}
local staminaDepleted = false
local sprinting = false
local isCrouching = false
local isMoving = false
local movementKeys = {Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D}
-- // Functions \\ --
local function tweenFov(duration, fov)
local tween = TweenService:Create(Camera, TweenInfo.new(duration, Enum.EasingStyle.Quad), { FieldOfView = fov })
tween:Play()
tween.Completed:Wait()
tween:Destroy()
end
local function tweenStaminaBar()
local stamina = StaminaManager.GetStamina()
local tween = TweenService:Create(staminaBar, TweenInfo.new(0.25, Enum.EasingStyle.Quad), { Size = UDim2.new(stamina / maxStamina, 0, 1, 0) })
tween:Play()
end
local function startSprinting()
if not sprinting and StaminaManager.GetStamina() > 0 and not isCrouching then
sprinting = true
StaminaManager.ReduceStamina(staminaDrainRate * RunService.RenderStepped:Wait()) -- Start stamina consumption
Humanoid.WalkSpeed = sprintSpeed
animTrack:Play()
tweenFov(0.25, sprintFov)
tweenStaminaBar()
end
end
local function stopSprinting()
if sprinting then
sprinting = false
Humanoid.WalkSpeed = defaultSpeed
animTrack:Stop()
tweenFov(0.25, normalFov)
staminaDepleted = false
tweenStaminaBar()
end
end
local function crouch()
if not isCrouching and not sprinting then
isCrouching = true
Humanoid.WalkSpeed = crouchSpeed
HumanoidRootPart.Size = Vector3.new(HumanoidRootPart.Size.X, crouchHeight, HumanoidRootPart.Size.Z)
end
end
local function stand()
if isCrouching then
isCrouching = false
Humanoid.WalkSpeed = normalSpeed
HumanoidRootPart.Size = Vector3.new(HumanoidRootPart.Size.X, normalHeight, HumanoidRootPart.Size.Z)
end
end
-- // Main \\ --
Character.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
healthBar.Size = UDim2.new(Character.Humanoid.Health / 100, 0, 1, 0)
end)
UserInputService.InputBegan:Connect(function(input, processed)
if processed then return end
if input.KeyCode == sprintKey then
shiftPressed = true
if next(movementKeysPressed) ~= nil then
startSprinting()
end
elseif table.find(movementKeys, input.KeyCode) then
movementKeysPressed[input.KeyCode] = true
if shiftPressed and StaminaManager.GetStamina() > 0 and not isCrouching then
startSprinting()
end
elseif input.KeyCode == crouchKey and not isCrouching and not sprinting then
crouch()
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == sprintKey then
shiftPressed = false
stopSprinting()
elseif table.find(movementKeys, input.KeyCode) then
movementKeysPressed[input.KeyCode] = nil
if next(movementKeysPressed) == nil then
stopSprinting()
end
elseif input.KeyCode == crouchKey then
stand()
end
end)
RunService.RenderStepped:Connect(function(deltaTime)
if sprinting then
if not StaminaManager.ReduceStamina(staminaDrainRate * deltaTime) then
stopSprinting()
end
end
tweenStaminaBar()
end)
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
isMoving = Humanoid.MoveDirection.Magnitude > 0
end)
local anim = game.ReplicatedStorage.Animations.Player.Land
local playAnim = script.Parent.Parent.Humanoid:LoadAnimation(anim)
Humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Landed then
playAnim:Play()
playAnim.Stopped:Wait()
if sprinting then
animTrack:Play()
end
end
end)
This revised script integrates the StaminaManager for managing stamina values and their regeneration, ensuring a central source of truth for stamina across potentially multiple scripts or parts of the game logic.
local Camera = workspace.CurrentCamera
local StaminaModule = require(script.Parent.StaminaModule)
local BV = Instance.new("BodyVelocity")
local function playDashAnimation()
local direction
for key, anim in MovementKeys do
if UIS:IsKeyDown(key) then
direction = anim
break
end
end
if direction then
Animations[direction]:Play()
end
end
local function dash()
local Now = workspace:GetServerTimeNow()
local DashCooldown = Character:GetAttribute("DashCooldown") or 0
if DashCooldown > Now or StaminaModule.GetStamina() < 50 then
return
end
Character:SetAttribute("DashCooldown", Now + Settings.Cooldown)
StaminaModule.ReduceStamina(50)
local NewBV = BV:Clone()
NewBV.MaxForce = Vector3.new(1e5, 1e5, 1e5)
NewBV.P = 1250
local Direction = Humanoid.MoveDirection
if Direction.Magnitude == 0 then
Direction = RootPart.CFrame.LookVector
end
local CamP0 = Camera.CFrame.LookVector
local RelativeDirection = CFrame.lookAt(CamP0, CamP0 + Direction).LookVector
local currentYVelocity = RootPart.Velocity.Y / 2
NewBV.Velocity = Vector3.new(RelativeDirection.X, 0, RelativeDirection.Z) * Settings.DashVelocity + Vector3.new(0, currentYVelocity, 0)
NewBV.Parent = RootPart
playDashAnimation()
task.delay(Settings.Lifetime, function()
NewBV:Destroy()
end)
end
UIS.InputBegan:Connect(function(InputObject: InputObject, SunkInput: boolean)
if SunkInput then return end
if not Keycodes[InputObject.KeyCode] then return end
dash()
end)
to this script when the player dashes, remove 50 of player's stamina. include the same 0.5 delay the sprint has.
To incorporate a stamina reduction with a delay for the dash functionality, we need to adjust the dash() function to check if enough time has elapsed since the last stamina-consuming action (similar to sprinting) before allowing another dash. Here’s how you can modify the existing script to include a stamina delay for dashing, drawing upon methods from the StaminaModule:
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid") :: Humanoid
local Animations = game:GetService("ReplicatedStorage"):WaitForChild("Animations"):WaitForChild("Player")
local Camera = workspace.CurrentCamera
local StaminaModule = require(script.Parent.StaminaModule)
local BV = Instance.new("BodyVelocity")
local Settings = {
Lifetime = 0.15;
DashVelocity = 55;
Cooldown = 3;
DashStaminaCost = 50;
}
local Keycodes = {
[Enum.KeyCode.E] = true;
[Enum.KeyCode.ButtonL1] = true;
}
local Animations = {
Forward = Humanoid:LoadAnimation(Animations:WaitForChild("FrontDash")),
Backward = Humanoid:LoadAnimation(Animations:WaitForChild("BackDash")),
Left = Humanoid:LoadAnimation(Animations:WaitForEtaChild("LeftDash")),
Right = Humanoid:LoadAnimation(Animations:WaitForChild("RightDash")),
}
local MovementKeys = {
[Enum.KeyCode.W] = "Forward",
[Enum.KeyCode.A] = "Left",
[Enum.KeyCode.S] = "Backward",
[Enum.KeyCode.D] = "Right"
}
local lastStaminaUseTime = 0 -- Add a tracker for the last stamina use time
local function playDashAnimation()
local direction
for key, anim in pairs(MovementKeys) do -- Corrected to pairs from MovementKeys
if UIS:IsKeyDown(key) then
direction = anim
break
end
end
if direction then
Animations[direction]:Play()
end
end
local function dash()
local Now = workspace:GetServerTimeNow()
local DashCooldown = Character:GetAttribute("DashCooldown") or 0
if DashCooldown > Now or StaminaModule.GetStamina() < Settings.DashStaminaCost then
return
end
-- Check if enough time has passed since last stamina-intensive action
if tick() - lastStaminaUseTime < 0.5 then
return
end
Character:SetAttribute("DashCooldown", Now + Settings.Cooldown)
StaminaModule.ReduceStamina(Settings.DashStaminaCost)
lastStaminaUseTime = tick() -- Update the last stamina use time
local NewBV = BV:Clone()
NewBV.MaxForce = Vector3.new(1e5, 1e5, 1e5)
NewBV.P = 1250
local Direction = Humanoid.MoveDirection
if Direction.Magnitude == 0 then
Direction = RootPart.CFrame.LookVector
end
local CamP0 = Camera.CFrame.LookVector
local RelativeDirection = CFrame.lookAt(CamP0, CamP0 + Direction).LookVector
local currentYVelocity = RootPart.Velocity.Y / 2
NewBV.Velocity = Vector3.new(RelativeDirection.X, 0, RelativeDirection.Z) * Settings.DashVelocity + Vector3.new(0, currentYVelocity, 0)
NewBV.Parent = RootPart
playDashAnimation()
task.delay(Settings.Lifetime, function()
NewBV:Destroy()
end)
end
UIS.InputBegan:Connect(function(InputObject, SunkInput)
if SunkInput then return end
if not Keycodes[InputObject.KeyCode] then return end
dash()
end)
This modified script now includes a delay similar to the sprint mechanic, ensuring that the player does not deplete their stamina too quickly by repeated dashing. It checks if enough time has passed since the last stamina use before proceeding with the dash. Adjust Settings.DashStaminaJerryCost to set how much stamina each dash should cost.