Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Staminabar can go down but it wont go back up after I let go of shift?

Asked by 6 years ago

(Note: game is filtered enabled) (Note: staminabar goes down but doesnt go back up) (Note: no errors in output)

-- Variables
local replicatedStorage = game:GetService("ReplicatedStorage")
local sprintEvent = replicatedStorage:WaitForChild("sprintEvent")
local sprintEnded = replicatedStorage:WaitForChild("sprintEnded")
local userInput = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
repeat wait() until plr.Character
local humanoid = plr.Character:WaitForChild("Humanoid")
local staminaFolder = plr:WaitForChild("stamFolder")
local stamina = staminaFolder:WaitForChild("stamina")
local staminaBar = script.Parent.background.staminabar
local staminaGui = script.Parent
local isSprinting = false
local canSprint = true


-- Scripting
userInput.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        isSprinting = true
        sprintEvent:FireServer()
        while isSprinting and stamina.Value > 0 do
            staminaBar:TweenSize(UDim2.new(stamina.Value * 0.01, 0, 1, 0), "Out", "Sine", .01)
            stamina.Value = stamina.Value - 1
            wait(.2)
        end
        if humanoid.WalkSpeed == 16 then
            isSprinting = false
        end
    end
end)


userInput.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        isSprinting = false
        sprintEnded:FireServer()
        while isSprinting and stamina.Value < 100 do
            stamina.Value = stamina.Value + 1
            staminaBar:TweenSize(UDim2.new(stamina.Value * 0.01, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, .01)
        end
        if stamina.Value >= 33 then
            canSprint = true
        elseif stamina.Value == 0 then
            canSprint = false
        end
    end
end)


1 answer

Log in to vote
0
Answered by 6 years ago

I haven't tested your code, but a problem could lie on line 38.

-- basicecstasy's code
userInput.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        isSprinting = false
        sprintEnded:FireServer()
        while isSprinting and stamina.Value < 100 do
            stamina.Value = stamina.Value + 1
            staminaBar:TweenSize(UDim2.new(stamina.Value * 0.01, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, .01)
        end
        if stamina.Value >= 33 then
            canSprint = true
        elseif stamina.Value == 0 then
            canSprint = false
        end
    end
end)

You have IsSprinting = false while the loop is running while isSprinting and stamina.Value < 100 do, the problem with this is it's saying While IsSprinting is True to continue through this loop. Try IsSprinting == falseon line 38 instead of while isSprinting and stamina.Value < 100 do

Also I never see you use "canSprint" Value on your top code, perhaps that would be useful to you in someway, whatever you do with that line is up to you.

Also I'm no expert on loops in functions, but if a new problem arises, say you can't sprint again after you stop sprinting perhaps you could try a coroutine.

Like I said, I haven't tested your code fully, but hopefully this helps you get somewhere.

Ad

Answer this question