So im having a problem with this movement script.
Specifically on the UIS.Input.Began:Connect
i want it to end when the Stamina Attribute hits 0
but just couldnt figure out how to. i tried breaking and setting the OnHold to false when the stamina hits 0
but as long as the player hold shift it’ll still run.
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local plr = Players.LocalPlayer
local char = plr.Character
local humanoid = char:WaitForChild("Humanoid")
local BaseSpeed = 8
local MaxSpeed = 20
local SpeedRate = .5
local DecayRate = -.1 --always negative
local OnHold = false
local W_Hold = false
local HoldTime = 0
local MaxStamina = 100
plr:SetAttribute("Stamina", 100)
local function UpdateStamina()
while true do
if not OnHold and plr:GetAttribute("Stamina") < MaxStamina then
local currentStamina = plr:GetAttribute("Stamina")
local AddStamina = math.min(currentStamina + 5, MaxStamina)
plr:SetAttribute("Stamina", AddStamina)
end
wait(3)
end
end
task.spawn(UpdateStamina)
UIS.InputBegan:Connect(function(input, gameProccessed)
if gameProccessed then return end
if input.KeyCode == Enum.KeyCode.W then
W_Hold = true
end
if input.KeyCode == Enum.KeyCode.LeftShift and W_Hold then
OnHold = true
HoldTime = 0
while OnHold and W_Hold do
print(stamina)
if stamina <= 0 then
OnHold = false
break
end
if plr:GetAttribute("Stamina") <= 1 then
break
end
local stamina = plr:GetAttribute("Stamina")
local MinusStamina = stamina - 1
plr:SetAttribute("Stamina",math.max(MinusStamina, 0) )
HoldTime += .1
local Speed = BaseSpeed * math.exp(SpeedRate * HoldTime)
Speed = math.clamp(Speed, BaseSpeed, MaxSpeed)
humanoid.WalkSpeed = Speed
local Delay = 1 / (Speed - BaseSpeed + 1)
wait(Delay)
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
W_Hold = false
end
if input.KeyCode == Enum.KeyCode.LeftShift then
OnHold = false
while humanoid.WalkSpeed > BaseSpeed do
local CurSpd = humanoid.WalkSpeed
local NewSpd = CurSpd * math.exp(DecayRate)
humanoid.WalkSpeed = math.max(BaseSpeed, NewSpd)
wait(.1)
end
end
end)