#Stamina Scripting failing in existing help
1 messages · Page 1 of 1 (latest)
local RunService = game:GetService("RunService")
local MaxStamina = script.Parent:GetAttribute("MaxStamina")
local CurrentStamina = script.Parent:GetAttribute("CurrentStamina")
local RunSpeed = script.Parent:GetAttribute("RunSpeed")
local WalkSpeed = script.Parent:GetAttribute("WalkSpeed")
local Exhausted = false
local Running = false
local CanRunValue = 25
local RecoverRate = 1
local DrainRate = 25
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UI = script.Parent
local Bar = UI.HolderFrame.SprintBackground.Bar
local function InputBegan(Input, GameProcess)
if Exhausted then return end
if Input.KeyCode == Enum.KeyCode.LeftControl and not GameProcess then
Humanoid.WalkSpeed = RunSpeed
Running = true
end
end
local function InputEnded(Input, GameProcess)
if Input.KeyCode == Enum.KeyCode.LeftControl and not GameProcess then
Humanoid.WalkSpeed = WalkSpeed
Running = false
end
end
local function UpdateStaminaUI()
Bar.Size = UDim2.new(math.clamp(CurrentStamina/MaxStamina, 0, 0.996), 0, 0.65,0)
end
RunService.Heartbeat:Connect(function(DeltaTime)
if Running and Humanoid.MoveDirection.Magnitude > 0 then
CurrentStamina = math.min(100, CurrentStamina - DrainRate * DeltaTime)
UpdateStaminaUI()
if CurrentStamina <= CanRunValue or CurrentStamina <= 0 then
Exhausted = true
end
elseif not Running then
CurrentStamina = math.min(100, CurrentStamina + RecoverRate * DeltaTime)
UpdateStaminaUI()
if CurrentStamina >= CanRunValue then
Exhausted = false
end
end
end)
UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputEnded:Connect(InputEnded)
The mains errors of this script are the fact that the player doesnt stop sprinting when the stamina ends, just when the player stops pressing left ctrl
and the stamina doesnt regen anymore and idk why
first problem is that you still can run after stamina runs out?
did i understand well
local RunService = game:GetService("RunService")
local MaxStamina = script.Parent:GetAttribute("MaxStamina")
local CurrentStamina = script.Parent:GetAttribute("CurrentStamina")
local RunSpeed = script.Parent:GetAttribute("RunSpeed")
local WalkSpeed = script.Parent:GetAttribute("WalkSpeed")
local Exhausted = false
local Running = false
local CanRunValue = 25
local RecoverRate = 1
local DrainRate = 25
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UI = script.Parent
local Bar = UI.HolderFrame.SprintBackground.Bar
local function InputBegan(Input, GameProcess)
if Exhausted then return end
if Input.KeyCode == Enum.KeyCode.LeftControl and not GameProcess then
Humanoid.WalkSpeed = RunSpeed
Running = true
end
end
local function InputEnded(Input, GameProcess)
if Input.KeyCode == Enum.KeyCode.LeftControl and not GameProcess then
Humanoid.WalkSpeed = WalkSpeed
Running = false
end
end
local function UpdateStaminaUI()
Bar.Size = UDim2.new(math.clamp(CurrentStamina/MaxStamina, 0, 0.996), 0, 0.65,0)
end
RunService.Heartbeat:Connect(function(DeltaTime)
if Running and Humanoid.MoveDirection.Magnitude > 0 then
CurrentStamina = math.min(100, CurrentStamina - DrainRate * DeltaTime)
UpdateStaminaUI()
if CurrentStamina <= CanRunValue or CurrentStamina <= 0 then
Exhausted = true
Humanoid.WalkSpeed = WalkSpeed
Running = false
end
elseif not Running then
CurrentStamina = math.min(100, CurrentStamina + RecoverRate * DeltaTime)
UpdateStaminaUI()
if CurrentStamina >= CanRunValue then
Exhausted = false
end
end
end)
UserInputService.InputBegan:Connect(InputBegan)
UserInputService.InputEnded:Connect(InputEnded)
also i do not think that this script will still work after the character dies
yeah
srry if i writed it wrong
oh
but i found another one right here
yeah u can help
which error?
by some reason the stamina doesnt regen when i press ctrl while i stand
wait
i will send a video give me a moment
do you mean
like this
like there is a line that prevents the player lose stamina while standing still
did you stop pressing ctrl?
i was pressing ctrl and the stamina bar just stop regen
and whether they pressed ctrl or not
yeah
like the player just lose stamina while walking
i mean the current script
yeah wait
you can see txt?
like .txt
oh
ill send just the runservice part
what
you see this
elseif not running then
you just change it to else only
elseif blabla -> else
dont touch anything else
only this
wait let me send the script
yea i coded it
if Running and Humanoid.MoveDirection.Magnitude > 0 then
CurrentStamina = math.max(0, CurrentStamina - DrainRate * DeltaTime)
UpdateStaminaUI()
if CurrentStamina <= CanRunValue or CurrentStamina <= 0 then
Exhausted = true
end
if CurrentStamina <= 0 then
Humanoid.WalkSpeed = WalkSpeed
Running = false
end
elseif not Running then
CurrentStamina = math.min(100, CurrentStamina + RecoverRate * DeltaTime)
UpdateStaminaUI()
if CurrentStamina >= CanRunValue then
Exhausted = false
end
end
end)```
here it is
elyeah
okay