Cronoss Posted February 22, 2022 Share Posted February 22, 2022 I'm trying to connect a "fuel-check" system to a speedometer resource. I know I'm doing something wrong but I can't understand how to get the "vehicle" without using "getPedOccupiedVehicle"... <- I noticed that this is already called in client-side (this is not my resource) I don't know if that's the problem but well, here is the code: First "getPedOccupiedVehicle" (client-side): function refreshVehicle() vehicle = getPedOccupiedVehicle(player) end addEventHandler("onClientRender", root, refreshVehicle) "getOccupiedVehicle" in server-side Event(check fuel system) function checkGas() veh = getPedOccupiedVehicle(player) x,y,z = getElementPosition(veh) if (veh) then Client-side triggering the checkGas function: function getCarStateColor() triggerServerEvent("verGasolina", root) if (fuelVeh) then local g = (255/1000) * health local r = 255 - g local b = 0 return r, g, b else return 0, 255, 0 end end Link to comment
βurak Posted February 22, 2022 Share Posted February 22, 2022 where exactly is the error occurring? can you show the rest of the code? Link to comment
Cronoss Posted February 22, 2022 Author Share Posted February 22, 2022 Exactly the console says "line 2, line 3", basically that part function checkGas() veh = getPedOccupiedVehicle(player) --------nil value, expected element x,y,z = getElementPosition(veh) -----------error, because "veh" now it's a boolean after that part, it is just sql requests and validation of "fuel": function checkGas() veh = getPedOccupiedVehicle(player) x,y,z = getElementPosition(veh) if (veh) then local plate = getVehiclePlateText(veh) local consulta = exports.mysql:_Query("SELECT * FROM vehicles WHERE plate=?", plate) if (consulta) then if (#consulta>0) then for _,column in ipairs(consulta) do ownerGet = tostring(column["owner"]) keyOW = tostring(column["keyOW"]) break end end end local flcheck = exports.mysql:_Query("SELECT * FROM vehicles WHERE owner=? AND keyOW=?", ownerGet,keyOW) if (flcheck) then if(#flcheck > 0) then for _,column in ipairs(flcheck) do fuelVeh = tonumber(column["fuelCar"]) engineVeh = tonumber(column["engineCar"]) break end end end Link to comment
βurak Posted February 22, 2022 Share Posted February 22, 2022 I think the player argument is invalid here, can you show me how to call this function? Link to comment
Cronoss Posted February 22, 2022 Author Share Posted February 22, 2022 The end of the "fuelcheck" in server side: addEvent("verGasolina", true) addEventHandler("verGasolina", root, checkGas, fuelVeh) The call in client-side: function getCarStateColor(fuelVeh) triggerServerEvent("verGasolina", root) if (fuelVeh) then local g = (255/1000) * health local r = 255 - g local b = 0 return r, g, b else return 0, 255, 0 end end Link to comment
βurak Posted February 22, 2022 Share Posted February 22, 2022 function checkGas() veh = getPedOccupiedVehicle(source) --here x,y,z = getElementPosition(veh) if (veh) then local plate = getVehiclePlateText(veh) local consulta = exports.mysql:_Query("SELECT * FROM vehicles WHERE plate=?", plate) if (consulta) then if (#consulta>0) then for _,column in ipairs(consulta) do ownerGet = tostring(column["owner"]) keyOW = tostring(column["keyOW"]) break end end end can you change the player to source Link to comment
Cronoss Posted February 22, 2022 Author Share Posted February 22, 2022 It says "Expected ped at argument 1, got root" Link to comment
βurak Posted February 22, 2022 Share Posted February 22, 2022 triggerServerEvent("verGasolina", localPlayer) can you change root to localPlayer 1 Link to comment
Cronoss Posted February 22, 2022 Author Share Posted February 22, 2022 It worked And, sorry if this is too much, but I want to ask, what's the problem here? function getCarStateColor(fuelVeh) triggerServerEvent("verGasolina", localPlayer) if (fuelVeh) then local g = (255/1000) * fuelVeh local r = 255 - g local b = 0 return r, g, b else return 0, 255, 0 end end I want to change the color of a icon with the fuelVeh value... I mean, if the fuel it's to low then the icon will turn "red", but it doesn't work properly. (The fuel system works, it turn off the veh if the fuel it's 0 all it's fine, that's why I think the problem it's in the function and not into the fuel-check system) 1 Link to comment
βurak Posted February 22, 2022 Share Posted February 22, 2022 (edited) Can you show me where this function is used. are you using dx as hud? i would like to see them if possible Edited February 22, 2022 by Burak5312 Link to comment
Cronoss Posted February 22, 2022 Author Share Posted February 22, 2022 (edited) Full client-side [1]: local smoothedRPMRotation = 0 local vehicleNos = nil local vehicleRPM = 0 -- 'main stats' function refreshVehicle() vehicle = getPedOccupiedVehicle(player) end addEventHandler("onClientRender", root, refreshVehicle) -- 'vehicle speed' function getVehicleSpeed() if (vehicle) then local vx, vy, vz = getElementVelocity(vehicle) if (vx) and (vy)and (vz) then return math.sqrt(vx^2 + vy^2 + vz^2) * 180 -- km/h else return 0 end else return 0 end end function getVehicleSpeedString() local speedString = math.floor(getVehicleSpeed() + 0.5) return string.format("%03d", speedString) end -- 'vehicle gear' function getVehicleGear() if (vehicle) then local vehicleGear = getVehicleCurrentGear(vehicle) return tonumber(vehicleGear) else return 0 end end function getFormattedVehicleGear() local gear = getVehicleGear() local rearString = "R" if (gear > 0) then return gear else return rearString end end -- 'vehicle rpm' function getVehicleRPM() if (vehicle) then if (isVehicleOnGround(vehicle)) then isVehicleInStunt = "false" if (getVehicleEngineState(vehicle) == true) then if(getVehicleGear() > 0) then vehicleRPM = math.floor(((getVehicleSpeed()/getVehicleGear())*180) + 0.5) if (vehicleRPM < 650) then vehicleRPM = math.random(650, 750) elseif (vehicleRPM >= 9800) then vehicleRPM = 9800 end else vehicleRPM = math.floor(((getVehicleSpeed()/1)*180) + 0.5) if (vehicleRPM < 650) then vehicleRPM = math.random(650, 750) elseif (vehicleRPM >= 9800) then vehicleRPM = 9800 end end else vehicleRPM = 0 end else isVehicleInStunt = "true" if (getVehicleEngineState(vehicle) == true) then vehicleRPM = vehicleRPM - 150 if (vehicleRPM < 650) then vehicleRPM = math.random(650, 750) elseif (vehicleRPM >= 9800) then vehicleRPM = 9800 end else vehicleRPM = 0 end end return tonumber(vehicleRPM) else return 0 end end function getRPMRoation() if (getVehicleRPM()) and (getVehicleRPM() >= 0) then local rpmRotation = math.floor(((270/9800)* getVehicleRPM()) + 0.5) if (smoothedRPMRotation < rpmRotation) then smoothedRPMRotation = smoothedRPMRotation + 2 end if (smoothedRPMRotation > rpmRotation) then smoothedRPMRotation = smoothedRPMRotation - 2 end return tonumber(smoothedRPMRotation) else return 0 end end function getFormattedRpmRotation() local rpm = getRPMRoation() local rpm1 = rpm local rpm2 = rpm local rpm3 = rpm local rpm4 = rpm if (rpm1 >= 90) then rpm1 = 90 elseif (rpm1 < 0) then rpm1 = 0 end if (rpm2 >= 180) then rpm2 = 180 elseif (rpm2 < 0) then rpm2 = 0 end if (rpm3 >= 219) then rpm3 = 219 elseif (rpm3 < 0) then rpm3 = 0 end if (rpm4 >= 360) then rpm4 = 360 elseif (rpm4 < 0) then rpm4 = 0 end return rpm1, rpm2, rpm3, rpm4 end function getCarStateColor(fuelVeh) triggerServerEvent("verGasolina", localPlayer) if (fuelVeh) then local g = (255/100.0000) * fuelVeh local r = 255 - g local b = 0 return r, g, b else return 0, 255, 0 end end function getNitroStateColor() local nitro = getVehicleUpgradeOnSlot(vehicle, 8) if (nitro > 0) then return 0, 255, 0 else return 75, 75, 75 end end Full client-side [2] (HUD): local screenWidth, screenHeight = guiGetScreenSize() -- 'Textures' local speedometerBG = "textures/speedometerBG.png" local speedometerBG2 = "textures/speedometerBG2.png" local speedometerNeedleWhite = "textures/speedometerNeedleWhite.png" local speedometerNeedleRed = "textures/speedometerNeedleRed.png" local speedometerOverlay = "textures/speedometerOverlay.png" local speedometerGlass = "textures/speedometerGlass.png" local carState = "textures/carState.png" local nitroState ="textures/nitroState.png" -- 'fonts' fontPrototype10 = dxCreateFont("fonts/Prototype.ttf", 10) fontPrototype40 = dxCreateFont("fonts/Prototype.ttf", 40) fontLCD22 = dxCreateFont("fonts/lcd.ttf", 22) function speedoMeterGUI() if (vehicle) then local rpm1, rpm2, rpm3, rpm4 = getFormattedRpmRotation() -- 'Speedometer' dxDrawImage(roundValue(screenWidth - 270), roundValue(screenHeight - 270), 200, 200, speedometerBG) dxDrawImage(roundValue(screenWidth - 270), roundValue(screenHeight - 270), 200, 200, speedometerNeedleRed, rpm4) dxDrawImage(roundValue(screenWidth - 270), roundValue(screenHeight - 270), 200, 200, speedometerNeedleWhite, rpm3) dxDrawImage(roundValue(screenWidth - 270), roundValue(screenHeight - 270), 200, 200, speedometerNeedleWhite, rpm2) dxDrawImage(roundValue(screenWidth - 270), roundValue(screenHeight - 270), 200, 200, speedometerNeedleWhite, rpm1) dxDrawImage(roundValue(screenWidth - 270), roundValue(screenHeight - 270), 200, 200, speedometerBG2) dxDrawText(getFormattedVehicleGear(), screenWidth - 160, screenHeight - 170, screenWidth - 160, screenHeight - 170, tocolor(255, 255, 255, 255), 1, fontPrototype40, "left", "top", false, false, false) dxDrawText("OOO", screenWidth - 160, screenHeight - 115, screenWidth - 160, screenHeight - 115, tocolor(25, 25, 25, 255), 1, fontLCD22, "left", "top", false, false, false) dxDrawText("***", screenWidth - 160, screenHeight - 115, screenWidth - 160, screenHeight - 115, tocolor(25, 25, 25, 255), 1, fontLCD22, "left", "top", false, false, false) dxDrawText(getVehicleSpeedString(), screenWidth - 160, screenHeight - 115, screenWidth - 160, screenHeight - 115, tocolor(255, 255, 255, 255), 1, fontLCD22, "left", "top", false, false, false) dxDrawText("km/h", screenWidth - 110, screenHeight - 95, screenWidth - 110, screenHeight - 95, tocolor(255, 255, 255, 255), 1, fontPrototype10, "left", "top", false, false, false) local nsr, nsg, nsb = getNitroStateColor() dxDrawImage(screenWidth - 108, screenHeight - 155, 24, 24, nitroState, 0, 0, 0, tocolor(nsr, nsg, nsb, 255), true) local csr, csg, csb = getCarStateColor() dxDrawImage(screenWidth - 108, screenHeight - 125, 24, 24, carState, 0, 0, 0, tocolor(csr, csg, csb , 255), true) dxDrawImage(roundValue(screenWidth - 270), roundValue(screenHeight - 270), 200, 200, speedometerOverlay) dxDrawImage(roundValue(screenWidth - 270), roundValue(screenHeight - 270), 200, 200, speedometerGlass) end end addEventHandler("onClientRender", root, speedoMeterGUI) Function (in server-side): function checkGas() veh = getPedOccupiedVehicle(source) x,y,z = getElementPosition(veh) if (veh) then local plate = getVehiclePlateText(veh) local consulta = exports.mysql:_Query("SELECT * FROM vehicles WHERE plate=?", plate) if (consulta) then if (#consulta>0) then for _,column in ipairs(consulta) do ownerGet = tostring(column["owner"]) keyOW = tostring(column["keyOW"]) break end end end local check = exports.mysql:_Query("SELECT * FROM vehicles WHERE owner=? AND keyOW=?", ownerGet,keyOW) if (check) then if(#check > 0) then for _,column in ipairs(check) do fuelVeh = (column["fuelofcar"]) ---------The "fuelveh" gets the table value representing the fuel of the vehicle motor = tonumber(column["motor"]) break end end end if (fuelVeh <= 0) then --------works, if fuelVeh 0 or less, then the vehicle turn off setVehicleEngineState(veh, false) mot = 0 ------new value to update local sendF = exports.mysql:_Query("UPDATE vehicles SET motor=? WHERE owner=? AND keyOW=?",mot,ownerGet,keyOW) end vehMove() --------function end end addEvent("verGasolina", true) addEventHandler("verGasolina", root, checkGas, fuelVeh) function vehMove() local fx,fy,fz = x+0.5, y+0.5,z+0.5 local fuelConsumption = 0.005 if (veh) then if (fuelVeh) then distance = getDistanceBetweenPoints3D(x,y,z,fx,fy,fz) enginePlus = 0 if getVehicleEngineState(veh) then enginePlus = 0.08 end local newFuel = (fuelVeh - (fuelConsumption*(distance+enginePlus))) local sendFuel = exports.mysql:_Query("UPDATE vehicles SET gasolina=? WHERE owner=? AND keyOW=?",newFuel,ownerGet,keyOW) else if (fuelVeh) then ---I think I messed up here fuelNew = (fuelVeh) else fuelVeh = 100 end end end end Edit = I added the hud part Edited February 22, 2022 by Cronoss Link to comment
βurak Posted February 22, 2022 Share Posted February 22, 2022 getCarStateColor takes fuel parameter but no parameter in onClientRender event getCarStateColor(fuelVeh) local csr, csg, csb = getCarStateColor() maybe this issue can be fixed if you pass vehicle fuel status into onClientRender event Link to comment
Cronoss Posted February 22, 2022 Author Share Posted February 22, 2022 How could i pass that parameter, triggering the server event in this? function refreshVehicle(fuelVeh) vehicle = getPedOccupiedVehicle(player) -----------trigger blabla end addEventHandler("onClientRender", root, refreshVehicle) Link to comment
βurak Posted February 22, 2022 Share Posted February 22, 2022 (edited) In fact, there is a very bad situation that I noticed in this resource, and that is this. Running triggerServerEvent per frame inside onClientRender event It will cause the server to consume a very large network connection and cause bad optimization of the server, so it is a very critical situation. so I suggest you delete this line function getCarStateColor(fuelVeh) --triggerServerEvent("verGasolina", localPlayer) -- <-- this if (fuelVeh) then local g = (255/100.0000) * fuelVeh local r = 255 - g local b = 0 return r, g, b else return 0, 255, 0 end end local csr, csg, csb = getCarStateColor() -- <-- this for fuel maybe you can use setElementData setElementData(veh, "Fuel", column["fuelofcar"]) --Create a key named "Fuel" and export the information from the database on client side use getElementData to get it getElementData(vehicle, "Fuel") Edited February 22, 2022 by Burak5312 1 Link to comment
Cronoss Posted February 22, 2022 Author Share Posted February 22, 2022 (edited) I understand the solution, but how I could make the "fuel" start to "decrease" if that event it's not added? edit: I mean the fuel-system, it starts to decrease when the player starts moving with the vehicle or/and the engine it's on Edited February 22, 2022 by Cronoss 1 Link to comment
βurak Posted February 22, 2022 Share Posted February 22, 2022 (edited) if you want to run this event you can do it with setTimer For example you set the timer to 1 second, on the client side you then pass the triggerServerEvent to this timer this is better than calling it during every frame Edited February 22, 2022 by Burak5312 1 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