How do I make jetpack fuel bar?

I am making a jetpack system, but I want a fuel GUI too but I don’t know how to script a fuel bar.

How would I make the fuel bar decrease and match the fuel amount?

Script so far:

local on = false
local plr = script.Parent.Parent.Parent

local function drainFuel()
	while true do
		wait()
		if on == true then
			wait(0.1)
			script.Parent.Fuel.Value -= 1
		end
	end
end

script.Parent.Activated:Connect(function()
	on = true
	script.Parent.Handle.jetpackThrust.MaxForce = Vector3.new(0, 7000, 0)
	drainFuel()
end)

script.Parent.Deactivated:Connect(function()
	on = false
	script.Parent.Handle.jetpackThrust.MaxForce = Vector3.new(0, 0, 0)
end)
1 Like

I’m assuming you have set this up within a tool, so I recreated this as shown here:

image

The jetpackThrust object is a VectorForce object, so I’ve changed “MaxForce” to “Force” in the script.

To create the fuel bar gui, you can set up a gui within the StarterGui folder:

image

To easily and accurately change the size of the fuel bar, have the background bar as its parent, and change the size of the fuel bar to {1, 0},{1, 0}. This means that the fuel bar will start with the same length and height as the background bar (see GuiObject | Roblox Creator Documentation for more info on how the “Size” property works).

You can easily change the fuel bar length to match the remaining fuel level by finding the fraction: (remaining fuel)/(maximum fuel). You would then need to set this fraction as the x scale of the fuel bar (i.e. {fraction, 0},{1, 0}).

Here is a very simple example:

local on = false
local plr = game:GetService('Players').LocalPlayer
local tool = script.Parent.Parent
local maxFuel = tool:WaitForChild('Fuel').Value -- Use WaitForChild() to wait for the fuel object to load. This will give us the starting (max) value of fuel

local fuelBarGui = plr.PlayerGui:WaitForChild('FuelGui') -- Use WaitForChild() to wait for the gui objects to load
local fuelBar = fuelBarGui:WaitForChild('BackgroundBar'):WaitForChild('FuelBar')

local function drainFuel()
	while true do
		wait()
		if on == true then
			wait(0.1)
			tool.Fuel.Value -= 1
			
			fuelBar.Size = UDim2.new(tool.Fuel.Value/maxFuel, 0, 1, 0)
		end
	end
end

tool.Activated:Connect(function()
	on = true
	tool.Handle.jetpackThrust.Force = Vector3.new(0, 7000, 0)
	drainFuel()
end)

tool.Deactivated:Connect(function()
	on = false
	tool.Handle.jetpackThrust.Force = Vector3.new(0, 0, 0)
end)

tool.Equipped:Connect(function()
	fuelBarGui.Enabled = true -- Enable gui on equip
end)

tool.Unequipped:Connect(function()
	fuelBarGui.Enabled = false -- Disable gui on unequip
end)

Which worked as expected for me :slight_smile:

image

Hope this helps!

local on = false
local plr = script.Parent.Parent.Parent

local function drainFuel()
while true do
wait()
if on == true then
wait(0.1)
script.Parent.Fuel.Value -= 1
end
end
end

script.Parent.Activated:Connect(function()
on = true
script.Parent.Handle.jetpackThrust.MaxForce = Vector3.new(0, 7000, 0)
drainFuel()
end)

script.Parent.Deactivated:Connect(function()
on = false
script.Parent.Handle.jetpackThrust.MaxForce = Vector3.new(0, 0, 0)
end)