Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

2 comments

0

thx for the help!

0

No Problem

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

This discussion is preserved as it was. New answers, comments, and votes are closed.