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

Stamina Amount goes down if player is standing still?

Hello!

I am currently trying to make a Stamina System for the Player, so that it won't be able to run forever. This works, however, if the player holds down shift without moving, the stamina bar goes down. I thought it may have been an issue caused by the UserInputService as it states that as soon as the key is held down to use stamina, however I am quite unsure and i am also unsure how to fix it.

Here is my script:

local UserInputService = game:GetService("UserInputService")
local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')

local Player = game:GetService("Players").LocalPlayer

local NormalWalkSpeed = 16
local NewWalkSpeed = 20

local Power = 10
local IsSprinting = false

repeat wait() until Player.Character

local Character = Player.Character

UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
	if GameProcessedEvent then return end
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = NewWalkSpeed
		IsSprinting = true
		while Power > 0 and IsSprinting do
			Power = Power - 0.03
			Bar:TweenSize(UDim2.new(Power / 10, 0, 1, 0), 'Out', 'Quint', 0.1, true)
			wait()
			if Power <= 0 then
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input, GameProcessedEvent)
	if GameProcessedEvent then return end
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = NormalWalkSpeed
		IsSprinting = false
		while Power < 10 and not IsSprinting do
			Power = Power + 0.03
			Bar:TweenSize(UDim2.new(Power / 10, 0, 1, 0), 'Out', 'Quint', 0.1, true)
			wait()
			if Power <= 0 then
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
			end
		end
	end
end)

If I could get some help with this, it would be great. Thanks!

1 answer

✓ Accepted answer

All you have to do is check if humanoid is running, the easiest way for that would be Humanoid.Running and a function to check that.

Here is a function that will set IsWalking variable to true or false whether the humanoid is running:

local Player = game:GetService('Players').LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild('Humanoid')

local IsWalking = false

Humanoid.Running:Connect(function(Speed)
	IsWalking = (Speed ~= 0)
end)

The first argument that the event returns is Speed, it is speed at which humanoid is running, all it does is sets IsWalking variable to whether Speed is 0 (Means he is not running) or it is not.

Now if you combine it with your function you would get

local UserInputService = game:GetService("UserInputService")
local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild('Humanoid')

local NormalWalkSpeed = 16
local NewWalkSpeed = 20

local Power = 10
local IsSprinting = false
local IsWalking = false

Humanoid.Running:Connect(function(Speed)
	IsWalking = (Speed ~= 0)
end)

UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
    if GameProcessedEvent then return end
    if Input.KeyCode == Enum.KeyCode.LeftShift then
	--/Return because player is not walking
	if not (IsWalking) then
		return;
	end

	Character.Humanoid.WalkSpeed = NewWalkSpeed

	...

Source > Humanoid.Running

1 comment

0

This worked very well. Thanks!

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