Need help with my fuel system

  1. What do you want to achieve?
    I’m trying to make a fuel system for a jetpack, which would subtract the fuel by 10 every second the player has been using it for. and add the fuel back by 10 every 3 seconds.

  2. What is the issue?
    the issue is, when i use the jetpack the fuel would get subtracted by random numbers it would start at 100 then 99, 97,90, 85, 78 until 0 or sometimes less(sometimes it would go over 100 when it’s refueling) I’ve also encountered something wrong with my script the coroutine would play an extra time after it’s yield.

  1. What solutions have you tried so far?
    YES, I’ve gotten no help i assume i wasn’t clear enough please respond to this post if I’m making no sense here.

this is just the subtracting part of the code i assume the adding part is just the same but just adding it

local fuel = 100

local function drainingFuel()
	local drainingFuelC = coroutine.wrap(function()
		local i = os.time()
		while wait(.5) do
			local info1 = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In,0,false,0)
			local goals1 = {Size = UDim2.new(fuel/100,0,1,0); }	
			local Healthup = TweenService:Create(ValueofJet, info1, goals1)	
			
			fuel += i - os.time()
			print(fuel, i)
			
			Healthup:Play()
			if not turnon or fuel == 0 then 
				Healthup:Play()
				break
			end
		end
		coroutine.yield()
	end)
	drainingFuelC()
end
2 Likes

Is this getting called multiple times?

FYI, there is a bit more efficient way of doing this without needing coroutine functions.

At the end of your script, you can have a while loop that only creates a new Tween when your fuel amount changes.

Here, I’m setting up a neater way of updating fuel and detecting a more authoritive way of actually subtracting fuel:

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local FUEL = 100
local function useFuel(amount)
    FUEL = math.max(0, FUEL-amount)
    
    local newTween = TweenService:Create(JetBar, 
        TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0),
        { Size = UDim2.new(FUEL/100 , 0 , 1, 0); }
    )
    newTween:Play()
end

-- code here...

-- somehwere near bottom of script
RunService:BindToRenderStepped("JetpackLoop", 101, function()
    if UserInputService:IsKeyDown(Enum.KeyCode.Space) and FUEL > 0 then
        useFuel(5)
    end
end)

Ideally, a good practice is to have only one loop at the bottom and have it do everything. Of course, there’s lots of ways to accomplish everything, but I feel it’s better to not use coroutine for what you’re trying to do. It can get messy quick if you’re not careful.

EDIT: The script I wrote will suck away your fuel in a short amount of time since it’s in a RenderStepped loop. Also, people with different rendering speeds will suck fuel faster than others. The best practice is to set the fuel to subtract depending on the time.

RunService:BindToRenderStepped("JetpackLoop", 101, function(delta)
    local f = 5 * delta -- this should be 5 fuels per second I believe
    if UserInputService:IsKeyDown(Enum.KeyCode.Space) and FUEL > 0 then
        useFuel(f)
    end
end)

EDIT2:
Since you’re updating it every render frame now, TweenService isn’t really needed anymore since it will be smooth. It’s up to you if you’d like to keep it.

3 Likes

okay so this is the brains of it, this is what activities when the jetpack is on or not, sorry for the messy code

local function handleAction(actionName, inputState, inputObject)
	if actionName == JetPackisOn and inputState == Enum.UserInputState.Begin and not overheated then
		script.Parent.Handle.JetVelocity.MaxForce = Vector3.new(0,math.huge,0)
		script.Parent.detail:WaitForChild("JetMesh").RightFlame.Fire.Enabled = true
		script.Parent.detail:WaitForChild("JetMesh").LeftFlame.Fire.Enabled = true
		script.Parent.Handle.Jetengine.Playing = true
		print("on")
		turnon = true
	end
	if inputState == Enum.UserInputState.End or humanoid.Sit == true then
		script.Parent.Handle.JetVelocity.MaxForce = Vector3.new(0,0,0)
		script.Parent.detail:WaitForChild("JetMesh").RightFlame.Fire.Enabled = false
		script.Parent.detail:WaitForChild("JetMesh").LeftFlame.Fire.Enabled = false
		script.Parent.Handle.Jetengine.Playing = false
		print("off")
		turnon = false
	end
end

If you want to incorporate this into what I provided, instead of checking whether the space bar is down, just check for when trunon is set to true.

1 Like

it’s giving an error (BindToRenderStepped is not a valid member of RunService “Run Service” - Client - LocalScript:112)

Ah yes, I misspelled it: BindToRenderStep

Anyway, what I gave you may not work out of the box. Try taking some time and understand everything I’m doing, then implement it into your own code your own way.

2 Likes