Jump to content

Citizen

Moderators
  • Posts

    1,803
  • Joined

  • Last visited

  • Days Won

    8

Everything posted by Citizen

  1. Both. function setFuelCar(thePlayer) local source = getPedOccupiedVehicle(thePlayer) -- (1) if getPedOccupiedVehicleSeat(thePlayer)==0 then -- (2) if getVehicleEngineState(source)==true then ---------if the engine it's on I start the timer -- (3) local x,y,z = getElementPosition(source) local fx,fy,fz = x+0.05, y+0.05,z+0.05 -- (4) local fuelConsumption = 0.900 local fuelVeh = getElementData(source, "Fuel") or 0 if (fuelVeh <= 0) then setVehicleEngineState(source, false) else timerF = setTimer( function () -- (5) local enginePlus = 0.200 local distance = getDistanceBetweenPoints3D(x,y,z,fx,fy,fz) -- (6) local newFuel = tonumber((fuelVeh - (fuelConsumption*(distance+enginePlus)))) setElementData(source, "Fuel", newFuel) end, 20000, 0) end end end end addEventHandler("onVehicleEnter", root, setFuelCar) You don't need to overwrite the source variable. It already exists thanks to the onVehicleEnter event. Have a look at: https://forum.multitheftauto.com/topic/33407-list-of-predefined-variables/#comment-337231 onVehicleEnter already gives you the seat number, you have to "grab" it too : function setFuelCar(thePlayer, seat) if seat == 0 then Here in your code, this check is only done once, when entering the vehicle. You have to change it so that you create the fuel timer anyway when entering the vehicle at seat 0 but inside the timer function, the 1st thing you do is to check the engine state. If it's ON, do the newFuel calculation thing. And if fuel is <= 0 then set it to OFF. (Why the +0.05 to each components ? seems totaly useless) You can't use a global variable on the server side or else if another player enters another vehicle, it will start his vehicle fuel timer and override the value in timerF so you have no way to kill the timer of your vehicle later. Must be local and "link it" with your vehicle element: setElementData(source, "fuelTimer", timerF, false) (false because there is no need to sync this value with all connected clients, only the server needs to know and manipulate it). You have to update the value of fx,fy,fz with the current x, y, z at the end to calculate the new distance traveled between point B and point C (20 secs later). Again, use element data to store that position for the next fuel consumption calculations (you can't use global variables).
  2. if player == localPlayer then return end UPS ! My bad, I meant ~= there if player ~= localPlayer then return end (replace in both) Okay yeah, here is your problem: You are creating 1 new timer per frame (x30 per sec at 30fps) so after 20 seconds of calmness you are calling triggerServerEvent every frame. Does that make sense ? What I would do is: -- on server side: listen to onVehicleEnter and when someone enters it, create the 20sec timer to update the fuel. Remember the timer (a table or a "not synced" element data and using the vehicle element as index/key to retreive it later) To update the fuel: make the necessary checks (engine state = ON etc) and use element data (element data get so much hate and I don't agree with all that hate, its perfect to sync an element data between server and clients when used correctly) local currentFuel = getElementData(vehicle, "fuel") or 0 setElementData(vehicle, "fuel", currentFuel - 5) -- updating the fuel on server Listen to onVehicleExit and get back the timer you created (and that you stored) and kill that timer -- on client side: Listen to onVehicleEnter and onVehicleExit to show or hide the speedometer (= add or remove the onClientRender) Yes you can use "onClientElementDataChange" to listen and update the fuel value updates for drawing local currentVeh = nil local currentFuel = 0 function checkDataFuel(dataName, oldValue, newValue) if dataName ~= "fuel" or source ~= currentVeh then return end -- cancel if it's not "fuel" update or if it's not for our vehicle currentFuel = newValue end addEventHandler("onClientElementDataChange", root, checkDataFuel) function enterVehicle(player, seat) if player ~= localPlayer then return end if seat == 0 or seat == 1 then currentVeh = source currentFuel = getElementData(source, "fuel") -- making sure we show the right value at 1st frame addEventHandler("onClientRender", root, drawSpeedo) -- show speedo isSpeedoVisible = true end end addEventHandler("onClientVehicleEnter", root, enterVehicle) function exitVehicle(player) if player ~= localPlayer then return end currentVeh = nil if isSpeedoVisible then removeEventHandler("onClientRender", root, drawSpeedo) -- hide speedo isSpeedoVisible = false end end addEventHandler("onClientVehicleStartExit", root, exitVehicle) function drawSpeedo() if not currentVeh then return end -- should never happen, just in case --[[ draw vehicle speed and fuel here. You have access to these variables to draw: currentVeh currentFuel ]] end I gave you a lot of client code to show you how it should look like with the new logic (your latest version was going a bit wild) For the server part, I let you try with the instructions I gave you. Note: After all that, the last optimization would be to not call getVehicleEngineState at every frame (in your getCarStateColor function) but it's a minor optimization, the biggest gain is to not spam the triggerServerEvent by implementing the new logic I'm suggesting.
  3. If you want to do that, I'd suggest you create another table that will store all players connection dates. Check this DB fiddle I made for you that does exactly what you want (pure SQL with the help of triggers) https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=a9ffcada462d8d1dff443bc0a1cf874d Ask any question.
  4. @Burak5312There is no issue to solve, the original code is working and has no perf problems. Also your code won't work, you forgot to flip the boolean inside show and hide functions. @CronossYour code is fine. And you are right, technically it's better because "onClientRender" won't call your lua function for nothing (if the speedometer is hidden). local isSpeedoVisible = false function drawSpeedometer() if not isSpeedoVisible then return end -- cancel immediatly -- drawing end addEventHandler("onClientRender", root, drawSpeedometer) ------ function A1(player) if player == localPlayer then return end -- you forgot this btw or else other players will trigger it too vehicle = getPedOccupiedVehicle(player) if getPedOccupiedVehicleSeat(player) == 0 or getPedOccupiedVehicleSeat(player) == 1 then isSpeedoVisible = true end end addEventHandler("onClientVehicleEnter", root, A1) function A2(player) if player == localPlayer then return end -- you forgot this btw or else other players will trigger it too isSpeedoVisible = false end addEventHandler("onClientVehicleStartExit", root, A2) If you had something like this, it is fine too and the perf are so close that the difference can be ignored (but technically it's better to add and remove the event handler like in your original post). If you are having perf issues with your speedometer, make sure you don't call get/set ElementData inside the drawing function. Those methods are expensive.
  5. The variable doesn't exist when the inner function is executed later (different scope). You have to send the player to the inner function (after the 3rd argument of setTimer): function changeWeapon(player) setPedAnimation(player, "colt45", "colt45_reload", 0, false, false, true, true) setTimer( function (timerPlayer) setPedAnimation(timerPlayer) end, 1000, 1, player) end addEvent("animCw", true) addEventHandler("animCw", root, changeWeapon) And since the argument you send through the setTimer call is exactly the one you send in setPedAnimation, you can make it shorter like so: function changeWeapon(player) setPedAnimation(player, "colt45", "colt45_reload", 0, false, false, true, true) setTimer(setPedAnimation, 1000, 1, player) end addEvent("animCw", true) addEventHandler("animCw", root, changeWeapon)
  6. Hello @Duff1995, Don't ask scripting questions in the english tutorials section. And as you are asking in portuguese, this post has been moved to Home > General MTA > Other languages > Portuguese / Português > Programação em Lua Thank you for your understanding.
  7. Hello @MTA.Castiel, The Tutorials section is only for posting tutorials. Your post is a scripting question so I'm moving it into the right section. Also please edit your post so that your Lua code is wrapped into a code block so that it's easier for people to read it: Thank you for your understanding.
  8. Hello and welcome, Don't ask your scripting questions in Tutorials please. I have moved it to the portuguese scripting section.
  9. Hello and welcome. This is not a tutorial and you are writing in portuguese so I have moved your post to https://forum.multitheftauto.com/forum/127-programação-em-lua/ Please keep the tutorial section for tutorials only. Thank you for your understanding.
  10. Hello @Duff1995, I moved your post in https://forum.multitheftauto.com/forum/127-programação-em-lua/. Please post in this section for your next scripting questions. Best regards, Citizen
  11. Hello, your post has been moved away from the tutorial section, to the scripting section. We want to keep only tutorial posts in the Tutorials sub-section. Regards,
  12. Citizen

    mta server list bug

    I don't know how still valid this list is but that's what I've found: Untill someone who knows the list replies. But I would first use our server port checker: https://nightly.multitheftauto.com/ports/ And if everything is fine, maybe your new node IP is banned by MTA (either an exact IP ban or an unfortunate IP range ban that include yours).
  13. Yes we did miss you ! Welcome back aboard ! You probably left but your were kinda still there with us as we grew with your popular resources. I think all servers have the bone_attach resource (or had at some point). At least I did grow with your bone_attach with me. When your website went down, it was a big hit for me, trying to find mirrors of it somewhere. But the community is always there to back it up and keep moving forward.
  14. [Moved from Scripting >Tutorials] Hello, Your post doesn't match the requirements to be considered a tutorial and is too short. You do not explain in details how to use it, the code shows bad practices, is not indented and actually has an error because Pdx is not defined. Also you must write the description in english in this category. Thanks for understanding.
  15. @Zango Nope, it will just stop working now (will never open) because the source of onClientMarkerHit is the marker which got hit, not the element hitting the marker. @Patlik You have to check if the hitPlayer (provided as 1st parameter of the handler function) is equal to the localPlayer element (and checking matchingDimenssion is a best practice too): function MKH_OPEN (element, matchingDimension) if element ~= localPlayer or not matchingDimension then return end if not atm[source] then return end if guiGetVisible(GUIEditor.window[1]) then -- I've also removed `== true` there as it is redundant guiSetVisible(GUIEditor.window[1], false) showCursor(false) else guiSetVisible(GUIEditor.window[1], true) showCursor(true) end end addEventHandler("onClientMarkerHit", root, MKH_OPEN)
  16. You have the most optimized way of creating the teams. But this code teams = createTeam is wrong, as you are overriding the teams variable as you are iterating over that list. The 1st one gets created, the second should fail because that teams variable just changed for the team entity you created in the 1st iteration of the loop. You can also write it like this (by using unpack): for k, v in ipairs( teams ) do createTeam( unpack(v) ) -- unpack can be used here because it is in the same order end but it's less optimized (if this is not a time critical code, then it will just save you time writing the code, but not by much either :p)
  17. Use the ipb and/or performancebrowser resource to check the perfs and get insights of what function calls makes your server to lag. Most of the time it is because you have too much calls to setElementData A little page about performancebrowser: https://wiki.multitheftauto.com/wiki/Resource:Performancebrowser
  18. Moved to Scripting section.
  19. Okay alors maintenant, si je te dis de modifier ton code pour essayer ça: else outputChatBox( row["x"] ) -- même résultat avec outputChatBox( row.x ) -- local x, y, z = getElementPosition(source) -- setElementPosition(source, x, y, z) end qu'est-ce qui s'affiche dans la chatbox ?
  20. Mes commentaires/explications dans le code: local qh = dbQuery(db, "SELECT * FROM users WHERE username=?", username) local result = dbPoll (qh, -1) local row = result[1] -- ici tu récupères la row de ta DB if not row then spawnPlayer(source, 1743.63281, -1861.50757, 13.57743) local x, y, z = getElementPosition(source) dbExec(db, 'INSERT INTO users (username, posX, posY, posZ) VALUES (?,?,?,?)', username, x, y, z) else -- ici t'as confirmation que "row" existe mais tu ne fais rien avec, il faut récupérer les valeurs x, y, z depuis la row ! local x, y, z = getElementPosition(source) -- et non pas getElementPosition qui va récupérer la position actuelle du joueur, cad 0, 0, 0 par défaut quand on se connecte setElementPosition(source, x, y, z) -- et il faut utiliser spawnPlayer plutôt que setElementPosition si t'as pas déjà spawn end
  21. C'est pourtant ce que tu as laissé sous-entendre en le présentant comme ton nouveau système d'inventaire ? J'ai déjà croisé pas mal de personnes de mauvaise fois mais là c'est quand même pas mal. Surtout que tu as report mon 1er message et une partie des admins/modo l'ont vu et envers lesquels j'ai dû me justifier. Pas longtemps cela dit car il était évident que j'avais vu juste. Ne vient pas te plaindre maintenant que tout le monde semble être contre toi. Tu as essayé de nous la mettre à l'envers et peut être tenté de gagner de la reconnaissance en tant que scripter. Tu ne sembles même pas être désolé et tu restes sur la défensive en essayant de nous faire croire que c'est nous qui avons mal interprété tes dires. Donc tu peux t'estimer heureux de t'en tirer sans même un warning (jusqu'à maintenant du moins ?)
  22. Idem je n'ai même pas été notifié ? (je supprime le 1er post)
  23. Screenshot illisible, impossible de se faire une bonne idée du système. De plus je doute fortement que t'ai pu faire ce système. Modifier un qui existe ne permet pas de se l'approprier. Je ne dis pas que c'est ce que tu as fait mais j'ai des gros doutes. En tout cas il faut plus de détails. Le mieux serait une vidéo qui montre les features.
  24. Disregard what I have said then. You didn't follow the setup instructions correctly: https://github.com/mabako/mta-paradise/blob/master/INSTALL.md
×
×
  • Create New...