Any help with my gas script?

So I have this script that sees if the player is pressing w a s d but it keeps going when you unpress the buttons, here is the script.

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local hum = player.Character:WaitForChild("Humanoid")
local fill = script.Parent
local stamina = 100
local canRun = true
running = false

UserInputService.InputBegan:connect(function(key, gameProcessedEvent)
	if not gameProcessedEvent and canRun and key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.A or key.KeyCode == Enum.KeyCode.S or key.KeyCode == Enum.KeyCode.D then
		running = true
		while running and stamina > 0 do
			fill:TweenSize(UDim2.new(stamina * 0.01, 0, 1, 0), "Out", "Quad", 0.5)
			stamina = stamina - 3
			wait(0.5)
		end
	end
end)


UserInputService.InputEnded:connect(function(key, gameProcessedEvent)
	if not gameProcessedEvent and key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.A or key.KeyCode == Enum.KeyCode.S or key.KeyCode == Enum.KeyCode.D then
		running = false		
	end
end)

while true do
	if not running and stamina < 100 then
		fill:TweenSize(UDim2.new(stamina * 0.01, 0, 1, 0), "Out", "Quad", 0.5)
	end
	if stamina >= 33 then
		canSprint = true
	elseif stamina == 0 then
		canSprint = false	
	end
	wait(0.5)
end

Right above the

running = true

try adding

If running then return end

That way if more than one button is hit it doesn’t run the while loop again

2 Likes

This is working thanks :smiley: character

2 Likes