Losing stamina while standing on a spot

I’m standing on a spot and holding shift and I start to lose stamina but I’m not supposed to lose stamina.

Stamina Script:

local player = game.Players.LocalPlayer
local char = player.Character

local UIS = game:GetService(“UserInputService”)

local stamina = 250
local isRunning = false

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

UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
isRunning = true
char.Humanoid.WalkSpeed = 20

	while stamina > 0 and isRunning do
		stamina = stamina -1
		script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear",0)
		wait()
		if stamina <= 0 then
			char.Humanoid.WalkSpeed = 8
		end

	end
end

end)

UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
isRunning = false
char.Humanoid.WalkSpeed = 8

	while stamina < 250 and not isRunning do
		stamina = stamina +0.5
		script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear",0)
		wait()
		if stamina <= 0 then
			char.Humanoid.WalkSpeed = 8
		end

	end
end

end)

3 Likes

So your issue is that standing still they still lose stamina?
Maybe you could check if they are moving or on every loop you run “is the players position in a different spot thats far enough to count as moving?”

1 Like

no, they aren’t moving. when i’m testing my game, and i hold shift and stand still, stamina is being lost

1 Like

Make a check to see if they’re moving before the stamina is being removed, you should be able to do that with this:

if humanoid.MoveDirection.Magnitude > 0 then

(if it doesn’t work let me know, i’ll look into a solution for you)

1 Like

where should i put that in the script

1 Like

Can you paste your entire script in a code block for me so I can edit it?
The post you made doesn’t have it all in a code block so it’s hard for me to work with

1 Like
local player = game.Players.LocalPlayer
local char = player.Character

local UIS = game:GetService("UserInputService")

local stamina = 250
local isRunning = false

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

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = true
		char.Humanoid.WalkSpeed = 20

		while stamina > 0 and isRunning do
			stamina = stamina -1
			script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear",0)
			wait()
			if stamina <= 0 then
				char.Humanoid.WalkSpeed = 8
			end

		end
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = false
		char.Humanoid.WalkSpeed = 8

		while stamina < 250 and not isRunning do
			stamina = stamina +0.5
			script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear",0)
			wait()
			if stamina <= 0 then
				char.Humanoid.WalkSpeed = 8
			end

		end
	end
end)
1 Like
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local userinputservice = game:GetService("UserInputService")
local stamina = 250
local isRunning = false
stamina = math.clamp(stamina, 0, 250)

userinputservice.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if humanoid.MoveDirection.Magnitude > 0 then
			isRunning = true
			character.Humanoid.WalkSpeed = 20

			while stamina > 0 and isRunning do
				stamina = stamina - 1
				script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear", 0)
				wait()

				if stamina <= 0 then
					character.Humanoid.WalkSpeed = 8
				end
			end
		end
	end
end)

userinputservice.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		isRunning = false
		character.Humanoid.WalkSpeed = 8

		while stamina < 250 and not isRunning do
			stamina = stamina + 0.5
			script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear", 0)
			wait()

			if stamina <= 0 then
				character.Humanoid.WalkSpeed = 8
			end
		end
	end
end)

Let me know if this works, dont replace your code entirely with this, make a new script and put this in it and check, since I don’t know exactly what you’re working with

1 Like

i have other scripts in the game but I can’t put here because there’s quite a few.

Used the code and it worked. Thanks a lot mate!

No problem, glad it worked, if you need any further help reply to this thread :slight_smile:

Can I add you on dc because I’m not the best at scripting and could use a bit of random help.

Sure, my username is oapa, i might be a bit slow to reply but ill help as much as possible

your account isn’t taking requests add me uniqueuserr