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

The function for the staminabar only works "once". Can someone help?

Asked by 2 years ago

First of all the staminabar, which is a frame, doesn't "react" when you press LeftShift first and then the W key. Then sometimes it gets stuck when its at 0 stamina and you can run forever. The last problem is that you can drain your stamina while standing still. Please someone help! Here's the code:

local character = player.Character
local hum = character:FindFirstChild("Humanoid")

local UserInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

local stamina = 200
local running = false

stamina = math.clamp(stamina,0,100)

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        running = true
        character.Humanoid.WalkSpeed = 14
        while stamina > 0 and running do
            stamina = stamina - 1
            script.Parent:TweenSize(UDim2.new(stamina / 100,0,1,0), "Out","Linear", 0)
            wait()
            if stamina == 0 then
                character.Humanoid.WalkSpeed = 8
            end
        end         
    end
end)

UserInputService.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        running = false
        character.Humanoid.WalkSpeed = 8
        while stamina < 100 and not running do
            stamina = stamina + .5
            script.Parent:TweenSize(UDim2.new(stamina / 100,0,1,0), "Out","Linear", 0)
            wait()
            if stamina == 0 then
                character.Humanoid.WalkSpeed = 8
            end
        end
    end
end)

If you want to test it, place the localscript under a staminabar frame in startergui. Thanks!

2 answers

Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
2 years ago
Edited 2 years ago

Hello,

There were a couple of problems I saw in your code which I'm not really gonna explain so I made a new code for you!

It's only a local script though

local PlayersService = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = PlayersService.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local staminaBar = script.Parent:FindFirstChild("StaminaBar")
local bar = staminaBar:FindFirstChild("Bar")

local stamina = 200
local maxStamina = 200

local isRunning = false

coroutine.resume(coroutine.create(function()
    while true do
        if isRunning then

            stamina -= 1

        else
            if stamina < maxStamina then
                stamina += 1
            end
        end

        wait()
    end 
end))

coroutine.resume(coroutine.create(function()
    RunService.Heartbeat:Connect(function()

        bar:TweenSize(UDim2.new(stamina/maxStamina, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0)

    end)
end))

coroutine.resume(coroutine.create(function()
    RunService.Heartbeat:Connect(function()

        if stamina < 10 then
            isRunning = false
            char:FindFirstChild("Humanoid").WalkSpeed = 16
        end

    end)
end))

UserInputService.InputBegan:Connect(function(key, gameProcessed)
    if key.KeyCode == Enum.KeyCode.LeftShift then
        if not gameProcessed then
            if stamina > 10 then

                isRunning = true
                char:FindFirstChild("Humanoid").WalkSpeed = 24

            else
                isRunning = false
                char:FindFirstChild("Humanoid").WalkSpeed = 16

            end
        else
            print("Is chatting!")

        end     
    end
end)

UserInputService.InputEnded:Connect(function(key, gameProcessed)
    if key.KeyCode == Enum.KeyCode.LeftShift then
        if not gameProcessed then

            isRunning = false
            char:FindFirstChild("Humanoid").WalkSpeed = 16

        else
            print("Is chatting!")

        end     
    end
end)
0
thx for the help! VelocityVH 6 — 2y
0
No Problem MattVSNNL 620 — 2y
Ad
Log in to vote
0
Answered by
enes223 327 Moderation Voter
2 years ago

hey you! have you ever heard of enes? if you are in trouble, better call enes!

Answer this question