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)