Rocket script is not adding extra fuel when there is clearly a fuel value to add

This script is for a rocket, it launches upon a RemoteEvent firing and flies up for 1 second per fuel. However, during pre-launch, the startup_fuel function is not working as intended. It does not add the 10 extra fuel found in the fuel tank.

Explorer:
explorerimage

Script:

local remotes = game.ReplicatedStorage.Remotes
local startup = remotes.RocketStartup

local launched = false

local fuel = 3
local altitude = 0
local usage = 0

function startup_fuel()
	for i,v in pairs(script.Parent:GetDescendants()) do
		if string.find(v.Name, "Fuel") then
			if v:FindFirstChild("Fuel") and v:IsA("NumberValue") then
				local f = v:FindFirstChild("Fuel")
				fuel += f.Value
				print(v.Name.." registered")
			end
		end
	end
end

function startup_engines()
	for i,v in pairs(script.Parent:GetDescendants()) do
		if string.find(v.Name, "Engine") then
			if v:FindFirstChild("Usage") then
				engines = v
				local u = v:FindFirstChild("Usage")
				usage += u.Value
				print(v.Name.." registered")
			end
		end
	end
end

function startup_seat()
	seat = script.Parent.Seat.Pilot
end


startup.OnServerEvent:Connect(function(plr)
	startup_seat()
	if seat.Occupant.Parent.Name == plr.Name then
		startup_engines()
		startup_fuel()
		
		wait(0.5)
		launched = true
		engines.Base.VectorForce.Enabled = true

		while fuel ~= 0 do
			fuel -= usage
			print(fuel)
			wait(1)
		end

		if fuel == 0 then
			engines.Base.VectorForce.Enabled = false
		end
	end
end)
Blind Guess
local remotes = game.ReplicatedStorage.Remotes
local startup = remotes.RocketStartup

local launched = false

local fuel = 3
local altitude = 0
local usage = 0

function startup_fuel()
    for _, v in pairs(script.Parent:GetDescendants()) do
        if string.find(v.Name, "Fuel") and v:IsA("NumberValue") then
            fuel = fuel + v.Value
            print(v.Name.." registered")
        end
    end
end

function startup_engines()
    local engines
    for _, v in pairs(script.Parent:GetDescendants()) do
        if string.find(v.Name, "Engine") and v:FindFirstChild("Usage") then
            engines = v
            usage = usage + v.Usage.Value
            print(v.Name.." registered")
        end
    end
end

function startup_seat()
    seat = script.Parent.Seat.Pilot
end

startup.OnServerEvent:Connect(function(plr)
    startup_seat()
    if seat.Occupant.Parent.Name == plr.Name then
        startup_engines()
        startup_fuel()

        wait(0.5)
        launched = true
        engines.Base.VectorForce.Enabled = true

        while fuel > 0 do
            fuel = fuel - usage
            print(fuel)
            wait(1)
        end

        engines.Base.VectorForce.Enabled = false
    end
end)

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.