Matevsz Posted July 9 Share Posted July 9 Hello, I created a fuel system, but after a while the game starts to lag. Is there something wrong with the code? Yes, I know I still need to add refueling, but I'm trying to figure out why it's lagging so much Fuel gauge needle angle (fuelNeedle): - 33 full - 128 low fuel - 150 empty Client local screenX, screenY = guiGetScreenSize() local scale = math.min(screenX/1680, screenY/1050) local function dxDrawRelativeImage(x, y, w, h, image, r, g, b, a, postGUI) return dxDrawImage(x * scale, y * scale, w * scale, h * scale, image, r, g, b, a, postGUI) end local fuelNeedle = "Fuel/DALTSHORT.png" local fuelImage = "Fuel/fuelimage.png" local lowFuelAngle = 128 local warningShown = false function fuelToAngle(fuel) -- fuel = 0.0 to 1.0 return 33 + (1 - fuel) * 117 end function angleToFuel(angle) return math.max(0, math.min(1, 1 - (angle - 33) / 117)) end function drawFuelGauge() local vehicle = getPedOccupiedVehicle(localPlayer) if not vehicle or getVehicleType(vehicle) ~= "Plane" then return end local fuel = getElementData(vehicle, "plane:fuel") or 0.18 local angle = fuelToAngle(fuel) dxDrawRelativeImage(899, 741, 249, 244, fuelImage) dxDrawRelativeImage(899 + 15, 741 + 25, 176, 195, fuelNeedle, angle) if angle >= lowFuelAngle and not warningShown then -- low fuel warning warningShown = true elseif angle < lowFuelAngle then warningShown = false end end addEventHandler("onClientRender", root, drawFuelGauge) addEventHandler("onClientKey", root, function(key, press) if key == "W" and press and currentVehicle and isPedInVehicle(localPlayer) then local fuel = getElementData(vehicle, "plane:fuel") or 0 if fuel <= 0 then cancelEvent() -- plane is gliding without speed end end end) Server local fuelUsePerSecond = 1 / 3600 function angleToFuel(angle) return math.max(0, math.min(1, 1 - (angle - 33) / 117)) end addEventHandler("onVehicleEnter", root, function(player, seat) if seat == 0 and getVehicleType(source) == "Plane" then local currentFuel = getElementData(source, "plane:fuel") if currentFuel == nil or currentFuel == 0 then local fuel = angleToFuel(128) setElementData(source, "plane:fuel", angleToFuel(128)) -- low fuel end end end) setTimer(function() for _, vehicle in ipairs(getElementsByType("vehicle")) do if getVehicleType(vehicle) == "Plane" and getVehicleEngineState(vehicle) then local driver = getVehicleOccupant(vehicle) if driver then local fuel = getElementData(vehicle, "plane:fuel") or 0.18 fuel = fuel - fuelUsePerSecond if fuel < 0 then fuel = 0 end setElementData(vehicle, "plane:fuel", fuel) end end end end, 1000, 0) Link to comment
Moderators IIYAMA Posted July 12 Moderators Share Posted July 12 On 09/07/2025 at 15:36, Matevsz said: but after a while the game starts to lag Might be because the (player/server) network can't catchup. -- (un)subscribe to the vehicle fuel element data of a specific vehicle addEventHandler("onVehicleEnter", getRootElement(), function(thePlayer, seat) if seat~=0 then return end -- Wiki Note: Before using addElementDataSubscriber you need to setup an initial value of element data in "subscribe" mode, otherwise the subscriber will not be added. if not getElementData(source, "plane:fuel") and getVehicleType(source) == "Plane" then setElementData(source, "plane:fuel", 0.18, "subscribe", "deny") end addElementDataSubscriber(source, "plane:fuel", thePlayer) end) addEventHandler("onVehicleExit", getRootElement(), function(thePlayer, seat) if seat~=0 then return end removeElementDataSubscriber(source, "plane:fuel", thePlayer) end) setTimer(function() -- Start with all players instead of all vehicles, because there are often more vehicles than players local players = getElementsByType("player") for i=1, #players do local player = players[i] local vehicle = getPedOccupiedVehicle(player) if vehicle and getVehicleType(vehicle) == "Plane" and getVehicleEngineState(vehicle) then local fuel = getElementData(vehicle, "plane:fuel") or 0.18 if fuel > 0 then -- do not update if fuel is 0 or lower, else you are wasting bandwidth by setting the data to the same value fuel = fuel - fuelUsePerSecond if fuel < 0 then fuel = 0 end setElementData(vehicle, "plane:fuel", fuel, "subscribe", "deny") end end end end, 1000, 0) Please let me know if the addElementDataSubscriber function works as expected, haven't used it myself yet. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now