So I've just recently adopted FilteringEnabled which required me to change my scripting habits which lead me to my issue here. I'm making a sprint that should take stamina after every second,
however, the function can be spammed if the player chooses to repeatedly press Shift. So I made a debounce but I am having trouble making it work:
--Damage Attribute
function subToAttribute(attribute, amount)
print("SubToAttribute()")
if attribute == "Health" then
if health - amount >= 0 then
health = health - amount
end
elseif attribute == "Stamina" then
if stamina - amount >= 0 then
stamina = stamina - amount
regenStamina()
end
end
RE_UpdateHUD:FireAllClients(player, {health, maxHealth}, {chakra, maxChakra}, {stamina, maxStamina})
end
function regenStamina()
if staminaRegenerating == false then
staminaRegenerating = true
local character = player.Character
character.Humanoid.Running:connect(detectPlayerMovement)
while stamina < maxStamina do
if staminaRegenPlayerMoving == false then
print("Stamina Regenerated")
addToAttribute("Stamina", 1)
RE_UpdateHUD:FireAllClients(player, {health, maxHealth}, {chakra, maxChakra}, {stamina, maxStamina})
else
print("Is Moving, HALT")
wait(5)
end
wait()
end
staminaRegenerating = false
end
end
--Regen Stamina: Detect if Player Moved
function detectPlayerMovement(speed)
wait()
if speed > 16 then
staminaRegenPlayerMoving = true
else
print("Stopped")
staminaRegenPlayerMoving = false
end
end
-- ACTIONS
sprintingStopped = true
sprintingInProgress = false
--<Variables>
sprintSpeed = 32
staminaSprintingCost = 3
--<Remote Events and Remote Functions>
RE_StartSprinting = replicatedStorage.Actions.StartSprinting
RE_EndSprinting = replicatedStorage.Actions.EndSprinting
--<Functions>
--Sprinting
function startSprinting()
local character = player.Character
if sprintingStopped == true then
sprintingStopped = false
character.Humanoid.WalkSpeed = sprintSpeed
while sprintingStopped == false do
wait(1)
if sprintingInProgress == false then
sprintingInProgress = true
subToAttribute("Stamina", staminaSprintingCost)
sprintingInProgress = false
end
end
end
end
RE_StartSprinting.OnServerEvent:connect(startSprinting)
--Stop Sprinting
function endSprinting()
local character = player.Character
sprintingInProgress = false
sprintingStopped = true
character.Humanoid.WalkSpeed = 16
end
RE_EndSprinting.OnServerEvent:connect(endSprinting)
The issue is that the stamina doesn't always drain when it's expected to. When you hold shift (sprint) the stamina bar doesn't go down, however, if you run and rapidly let go of run (trigger endSprinting) and quickly go back into sprint mode then the stamina works. (you should really test for yourself to get a better picture).
If you need the client script:
player = game.Players.LocalPlayer
character = player.Character
sprintKey = Enum.KeyCode.LeftShift
stealthKey = Enum.KeyCode.LeftControl
RE_StartSprinting = replicatedStorage.Actions.StartSprinting
RE_EndSprinting = replicatedStorage.Actions.EndSprinting
-- SPRINTING
--<Variables>
fov = 70
camera = game.Workspace.CurrentCamera
sprintAnimation = script.Animations.Sprint
sprintAnimation = character.Humanoid:LoadAnimation(sprintAnimation)
-- Start Sprinting
function startSprinting(inputObject, gameProcessedEvent)
if inputObject.KeyCode == sprintKey then
RE_StartSprinting:FireServer(player)
-- Animation
sprintAnimation:Play()
--FOV Effects
for i=0, (fov-55) do
wait()
camera.FieldOfView = camera.FieldOfView + 1
end
camera.FieldOfView = fov
end
end
game:GetService("UserInputService").InputBegan:connect(startSprinting)
-- End Sprinting
function endSprinting(inputObject, gameProcessedEvent)
if inputObject.KeyCode == sprintKey then
RE_EndSprinting:FireServer(player)
-- Stop Animation
sprintAnimation:Stop()
-- Undo FOV Effects
for i=0, (fov-55) do
wait()
camera.FieldOfView = camera.FieldOfView - 1
end
camera.FieldOfView = 55
end
end
game:GetService("UserInputService").InputEnded:connect(endSprinting)
The hierarchy: http://imgur.com/a/G54iH
Explanation: The script 'Template' is where the sprinting script is. Template is cloned and placed under Lobby where it's renamed as the Player who joined.
Sorry that this is an absolute mess! Debouncing stuff is usually pretty simple, and I'm betting that this is just a stupid mistake that's hiding in plain sight111!

1 comment
Note: startSprinting is the function that should be requesting stamina to drain. This is the function with the debounce. sprintingStopped is the debounce that breaks the while loop to stop running, and sprintingInProgress is the debounce that tells the while loop not to spam the stamina drain.