Help! My jetpack is buggy(solved)

Hello so i’m trying to make sometype of fuel draining system and fuel recovering system (I’ve already done so but it’s really buggy).
for some reason the coroutine loop plays twice or more which drains the fuel really fast, then the refueling part works but it’s terrible. the GUI doesn’t really fill up when the fuel is at max.
I’ve just been looking at forums I couldn’t really do what some of these forum post solved since this is all one local script doing everything.

local ContextActionService = game:GetService("ContextActionService")
local JetPackisOn = "Jet"

local tool = script.Parent
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
	character = player.CharacterAdded:Wait()
end

local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
local shockAnimation = Instance.new("Animation")
shockAnimation.AnimationId = "rbxassetid://7727578379"
local TweenService = game:GetService("TweenService")

local shockAnimationTrack = Animator:LoadAnimation(shockAnimation)
shockAnimationTrack.Priority = Enum.AnimationPriority.Action
shockAnimationTrack.Looped = true
local turnon = false
local Clonefuel = script.Parent:SetAttribute("Fuel", 100)
local feul = script.Parent:GetAttribute("Fuel")
local ValueofJet = player.PlayerGui:WaitForChild("Fuel").FuelFrame.Fuel
local o, i = os.clock(), feul 
local drainingFuel = coroutine.create(function()  

	while true do 
		if not turnon or i <= 0 then coroutine.yield() end	 
		--//code, builtin loop
		wait(1) -- reliability depends on the amount of time you yield for
		local info1 = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In,0,false,0)
		local goals1 = {Size = UDim2.new(i/100,0,1,0); }	
		local Healthup = TweenService:Create(ValueofJet, info1, goals1)	
		Healthup:Play()
		i -= 5	
		print(i)       
	end
end)

local ReFuel = coroutine.create(function()  
	while true do 
		--//code, builtin loop
		wait(1) -- reliability depends on the amount of time you yield for
		local info1 = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In,0,false,0)
		local goals1 = {Size = UDim2.new(i/100,0,1,0); }	
		local Healthup = TweenService:Create(ValueofJet, info1, goals1)	
		Healthup:Play()
		i += 5
		print(i) 
		if turnon or i >= 100 then coroutine.yield() end
	end
end)

local function handleAction(actionName, inputState, inputObject)
	if actionName == JetPackisOn and inputState == Enum.UserInputState.Begin 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
		turnon = true
		coroutine.resume(drainingFuel) -- initiate
	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
		turnon = false
		coroutine.resume(ReFuel) -- initiate
	end
end


tool.Equipped:Connect(function ()
	ContextActionService:BindAction(JetPackisOn, handleAction, true, Enum.KeyCode.Space)
	shockAnimationTrack:Play()
	if not script.Parent.detail:FindFirstChild("JetMesh") then
		print("no")
		local addpart = script.Parent.JetMesh:Clone()
		addpart.Parent = script.Parent.detail
		addpart:PivotTo(character.UpperTorso.CFrame * CFrame.new(0,0,1))
		addpart.Orientation = character.UpperTorso.Orientation
		addpart.CanCollide = false
		addpart.CastShadow = false
		local weld = Instance.new("WeldConstraint", script.Parent.detail:WaitForChild("JetMesh"))
		weld.Part0 = addpart
		weld.Part1 = character.UpperTorso
		
		local addpart2 = script.Parent.StrapMesh:Clone()
		addpart2.Parent = script.Parent.detail
		addpart2:PivotTo(character.UpperTorso.CFrame)
		addpart2.Orientation = character.UpperTorso.Orientation
		addpart2.CanCollide = false
		addpart2.CastShadow = false
		local weld2 = Instance.new("WeldConstraint",script.Parent.detail:WaitForChild("StrapMesh"))
		weld2.Part0 = addpart2
		weld2.Part1 = addpart
		
	end
end)

tool.Unequipped:Connect(function ()
	ContextActionService:UnbindAction(JetPackisOn)
	shockAnimationTrack:Stop()
	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
	turnon = false
end)
2 Likes

What specifically is “buggy” and needs fixing, I’m not sure I fully understand.

so uh, everytime when i call either of the two coroutine (the refueling and draining fuel one) it would play the loop more than once like if there were a delay when I stop calling it. and everytime when I spam the space button the coroutine loops would go crazy like seen in the video

i don’t really know how to explain it that well, I’m just having trouble with the coroutines. That is what controls the fuel system and it’s just broken in a way i don’t really know how to explain it that well

i could reword this by saying is using coroutines for a fuel system a bad idea? i only used it since the loop doesn’t yield allowing the script to run still when it’s recharging or when it’s burning the fuel