Any help with gas deplete gui?

So I have this script that depletes a gas value, but I want this gui to show the progress of it going down? Any help? I dont know how i would go about this?

	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 = game.Workspace.Vehicles:FindFirstChild(player.Name.."Car").Driving.Gas.Value
	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

image image

1 Like

I haven’t read your code, but here’s the general idea.

local Percent = CurrentGas / TotalPossibleGas

Then size a GUI based on that, like so:

UDim2.new(Percent,0,1,0) -- Fills horizontally
UDim2.new(1,0,Percent,0) -- Fills vertically
1 Like

So i was able to put this together.

local CurrentGas = game.Workspace.Sedan.Driving.DriverSeat.Gas.Value

local TotalPossibleGas = 1000

local Percent = CurrentGas / TotalPossibleGas

local fill = script.Parent

fill:TweenSize(UDim2.new(Percent,0,1,0)) -- Fills horizontally

fill:TweenSize(UDim2.new(1,0,Percent,0)) -- Fills vertically

but it makes the bar of my gui go up and down when pressing new buttons. any help?

here is the script that makes the gas go down

ReverseGas = 1 --Change this number to edit how much gas is used when going backwards.

ForwardGas = 1 --Change this number to edit how much gas is used when going forwards.

seat = script.Parent

while true do

if seat.Throttle == 1 then

seat.Gas.Value = seat.Gas.Value - ForwardGas

elseif seat.Throttle == -1 then

seat.Gas.Value = seat.Gas.Value - ReverseGas

else

end

wait(10) --Edit this to change how quickly the script depletes gas.

end
1 Like

You don’t need to use the Vert fill and Horiz fill, I gave both to demonstrate how you would do either way depending on how your GUI is designed.

1 Like

My gui is designed to go down as the gas number goes down

Then use vertical filling.

local Gas = workspace.Sedan.Driving.DriverSeat.Gas

Gas:GetPropertyChangedSignal("Value"):Connect(function()
 local P = Gas.Value/1000
 fill:TweenSize(UDim2.new(1,0,P,0))
end)

what im asking for is that it decreases and stays down with the value, when it goes down, it goes goes up, but then when u go and press the buttons again, it goes back up (the gui)

That made no sense. I’m very confused now.

So basically, the script u gave me, when the gas ammount decreases by 1, the gui goes up the tiny bit, but then goes back to full instead of staying at the decreased ammount.

Some other script is probably altering the size of the fill GUI, because my script only alters it when the value changes- it wouldn’t move it back afterwards.

Either that, or you set the gas value back up.

this is the only other script that messed with stuff

ReverseGas = 1 --Change this number to edit how much gas is used when going backwards.

ForwardGas = 1 --Change this number to edit how much gas is used when going forwards.

seat = script.Parent

while true do

if seat.Throttle == 1 then

seat.Gas.Value = seat.Gas.Value - ForwardGas

elseif seat.Throttle == -1 then

seat.Gas.Value = seat.Gas.Value - ReverseGas

else

end

wait(10) --Edit this to change how quickly the script depletes gas.

end

See what prints.

local Gas = workspace.Sedan.Driving.DriverSeat.Gas

Gas:GetPropertyChangedSignal("Value"):Connect(function()
 local P = Gas.Value/1000
 fill:TweenSize(UDim2.new(1,0,P,0))
 print("Moved fill to",P,"since Gas was changed to",Gas.Value)
end)

Attempt to index global ‘fill’

Do I really have to spoon-feed that to you? You already had it in the version you gave me.

local fill = script.Parent
local Gas = workspace.Sedan.Driving.DriverSeat.Gas

Gas:GetPropertyChangedSignal("Value"):Connect(function()
 local P = Gas.Value/1000
 fill:TweenSize(UDim2.new(1,0,P,0))
 print("Moved fill to",P,"since Gas was changed to",Gas.Value)
end)

Ouch, but alright my bad… I will test this.

But its still doing the thing… where once it starts going again the fill goes right back to full

You’ve completely ignored what I asked. I asked for what the output is.

I’m done here. You’re unwilling to work with me on this, and you just want a genie to appear with code for you.

Good luck on your project, and I hope you solve this soon.

1 Like

There is no output errors! I have no idea of what else im supposed to say if there is none.

The only thing in output is when I change the value of the gas to 10, it says “Moved fill to 0.2 since gas was changed to 10” which is when i changed 1000 to 50.

I didn’t ask about errors, I asked for what the print statement we added was telling us.

That means my code only moved it once. If it moved again without printing, it’s because of something else you did.