Help with my gas not working?

So I have this gas code that when you press w a s d it will lose gas, but it does not work when put in a seat, any help?

	wait(1)
	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 = 1000
	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
			if running then return end
			running = true
			while running and stamina > 0 do
				fill:TweenSize(UDim2.new(stamina * 0.001, 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

1 Like

image

Wouldnt the
if running then return end
make the function end before doing the while loop, but im not sure

Also, its
if gameProcessedEvent then return end
Instead of what you put

EDIT:
Also for the keys just do a simple check after the GPE check:

if key.KeyCode == Enum.KeyCode.W or key.Keycode == Enum.KeyCode.A etc.......

I am confused?? What do I exchange with if gameProcessedEvent then return end

I meant where in the script???

The LocalScript will not run because it is in the seat (which is in Workspace).

https://developer.roblox.com/en-us/api-reference/class/LocalScript

2 Likes
game:GetService("UserInputService").InputBegan:Connect(function(input,gpe)
    if gpe then return end
    if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D then
        -- stuff here
    end
end)

Also just nest another if statement if you wanted to check for the Running bool

But its in the playergui? character30

??

	wait(1)
	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 = 1000
	local canRun = true
	running = false

game:GetService("UserInputService").InputBegan:Connect(function(input,gpe)
    if gpe then return end
    if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D then	
	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
			if running then return end
			running = true
			while running and stamina > 0 do
				fill:TweenSize(UDim2.new(stamina * 0.001, 0, 1, 0), "Out", "Quad", 0.5)
				stamina = stamina - 3
				wait(0.5)
				end
		end
		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

Try walking through your code with the debugger.