Hello everyone, I’ve got a horror game that I’m working on, and I made a sprint system for it. Therefore, I made it Shift to sprint, but for some reason it has a delay when you release the shift key, YET all the other keys are instantaneous.
UPDATE
I realized it wasn’t even delayed, the InputEnded function didn’t even fire until I released another key. I don’t know if this is a studio bug (I tried it in a baseplate, same results). I need help
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Modules = ReplicatedStorage.Modules
local TweenModule = require(Modules.TweenModule)
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Sprinting: BoolValue = Player:WaitForChild("Sprinting")
local Stamina: DoubleConstrainedValue = Player:WaitForChild("Stamina")
local SprintGui = script.Parent
local StaminaBackground = SprintGui.StaminaBackground
local SprintBar = StaminaBackground.Bar
local sPS = 20 -- Stamina Per Second
local regenPS = 8 -- Stamina Regenerated Per Second
local lastSprinted = tick()
local sprintRecoveryDelay = 2
local TurnoffQueue = false
RunService.RenderStepped:Connect(function(deltaTime)
local drain = sPS*deltaTime
local recover = regenPS*deltaTime
if Stamina.Value > 0 then
if Sprinting.Value then
Stamina.Value -= drain
else
if tick()-lastSprinted > sprintRecoveryDelay then
Stamina.Value += recover
end
end
else
if Sprinting.Value then
Sprinting.Value = not Sprinting.Value
lastSprinted = tick()
end
if tick()-lastSprinted > sprintRecoveryDelay then
Stamina.Value += recover
end
end
end)
Stamina.Changed:Connect(function()
SprintBar.Size = UDim2.fromScale(Stamina.Value/Stamina.MaxValue,1)
end)
Sprinting.Changed:Connect(function()
if Sprinting.Value then
TweenModule:Tween(StaminaBackground,{GroupTransparency = 0},0.4,"Sine")
else
if StaminaBackground.GroupTransparency < 1 and not TurnoffQueue then
TurnoffQueue = true
task.spawn(function()
repeat wait(1) until tick()-lastSprinted > 8
if not Sprinting.Value then
TurnoffQueue = false
TweenModule:Tween(StaminaBackground,{GroupTransparency = 1},0.7,"Circular")
end
end)
end
end
end)
UserInputService.InputBegan:Connect(function(input,typing)
if typing then return end
if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift or input.KeyCode == Enum.KeyCode.ButtonL3 then
if Humanoid.MoveDirection.Magnitude > 0 then
if Stamina.Value > 0 then
Sprinting.Value = true
TurnoffQueue = false
end
end
end
end)
UserInputService.InputEnded:Connect(function(input,typing)
--if typing then return end
warn("Ended")
if input.KeyCode == Enum.KeyCode.LeftShift then
Sprinting.Value = false
lastSprinted = tick()
end
end)
**
Help is appreciated! (quickly)
**