Search the Community
Showing results for tags 'element'.
-
Let's talk script security. It might be a taboo topic for some but it shouldn't, at all. In fact, the more people talk about this and unite to discuss and create robust, safe and well-coded scripts, the better the MTA resource development community will be. Rules: Main article: https://wiki.multitheftauto.com/wiki/Script_security The key points are there, but the code examples don't suffice, and the implementations will vary depending on the gamemode and resources. Pinned information: Coming soon Notes: - I'm not perfect and everyone makes mistakes. Please correct me when I'm wrong. - Eventually if this becomes a popular topic, I am willing to incorporate translations of certain explanations into other languages in this OP, as I know a majority of the MTA community doesn't master the English language and prefers to communicate and read in other languages.
-
Hello dear friends, I had an idea to create some idea related to the rotation of the element, let's say the dxDrawRectangle of the plate by 180 degrees and at the same time with a clear rotation effect! below is an example, this effect is needed on the player's reflection of this plate, namely on the screen on the graphic design, and not in the world, if you understand me of course
-
Im working on a script wich create flames on the Thrusters of a vehicle (Mammoth Thruster of GTA Online), works well if you are the only that has a Thruster, but if someone also has a Thruster and gets out the vehicle, your Thruster will keep the attached elements and will not destroy the flames if you get out of the vehicle: Here is a video of what is happening: https://www.youtube.com/watch?v=Q3z9fdnBG0w and serverside and clientside scripts that are involved Serverside: (Functions for creating the flames) function LightOn(vehicle, x, y, z) if thePlayer ~= localPlayer then return end light = createMarker ( x , y , z, "corona", 0.5, 255, 160, 0, 170 ) attachElements(light, vehicle, 0, -0.3, -0.8) end addEvent("lighton", true) addEventHandler("lighton", getRootElement(), LightOn) function LightOff() if thePlayer ~= localPlayer then return end if light then destroyElement(light) end end addEvent("lightoff", true) addEventHandler("lightoff", getRootElement(), LightOff) function ThrustersOn(vehicle, x, y, z) if thePlayer ~= localPlayer then return end superhitbox = createObject (3471, x , y , z) setElementAlpha(superhitbox, 0) firethrustl = createObject (2031, x , y , z) firethrustr = createObject (2031, x , y , z) attachElements(superhitbox, vehicle, 0, -10, -1) attachElements(firethrustl, vehicle, 0.47, -0.4, -0.2) attachElements(firethrustr, vehicle, -0.47, -0.4, -0.2) setElementCollisionsEnabled(firethrustl, false) setElementCollisionsEnabled(firethrustr, false) end addEvent("thrusterson", true) addEventHandler("thrusterson", getRootElement(), ThrustersOn) function ThrustersOff() if thePlayer ~= localPlayer then return end if not firethrustl then return end destroyElement(firethrustl) destroyElement(firethrustr) end addEvent("thrustersoff", true) addEventHandler("thrustersoff", getRootElement(), ThrustersOff) Clientside: (Functions that triggers serverside events when you are in the Thruster) Thruster = 465 function SitOnThruster(thePlayer) if thePlayer ~= localPlayer then return end local vehicle = getPedOccupiedVehicle(localPlayer) if ( vehicle and getElementModel (vehicle) == Thruster ) then bindKey("vehicle_fire", "down", shootProjectile) triggerServerEvent ( "onthruster", resourceRoot) local x, y, z = getElementPosition(vehicle) local h, m = getTime() triggerServerEvent("thrusterson", getRootElement(), vehicle, x, y, z) if h > 20 then triggerServerEvent("lighton", getRootElement(), vehicle, x, y, z) else return end end end addEventHandler ( "onClientVehicleEnter", root, SitOnThruster ) function Notsit(thePlayer) if thePlayer ~= localPlayer then return end if Thruster then triggerServerEvent("thrustersoff", getRootElement(), vehicle, x, y, z) triggerServerEvent("lightoff", getRootElement(), vehicle, x, y, z) else return end end addEventHandler ( "onClientVehicleExit", root, Notsit ) function killed() if Thruster then if (isPedInVehicle(localPlayer) and Cars[getElementModel(getPedOccupiedVehicle(localPlayer))]) then if getPedOccupiedVehicleSeat ( localPlayer ) == 1 then return end triggerServerEvent("thrustersoff", getRootElement(), vehicle, x, y, z) triggerServerEvent("lightoff", getRootElement(), vehicle, x, y, z) else return end end end addEventHandler ( "onClientPlayerWasted", localPlayer, killed ) function shootProjectile() if not disparado then local vehicle = getPedOccupiedVehicle(localPlayer) if ( vehicle and getElementModel (vehicle) == Thruster ) then if isVehicleOnGround(vehicle) == false then local x, y, z = getElementPosition(vehicle) projectile = createProjectile(vehicle, 15, x, y+4, z-10) disparado = true setTimer ( function() disparado = false end, 1500, 1 ) else local x, y, z = getElementPosition(vehicle) createProjectile(vehicle, 15, x, y, z-3) disparado = true setTimer ( function() disparado = false end, 1500, 1 ) end end end end I'm very confused, i'm trying to make the script only create element for each player.
- 2 replies
-
- destroyelement
- problem
-
(and 6 more)
Tagged with:
-
Map files Table of contents: Introduction How to read a map file? Broken map file, what to do? Editor bugged, what to do? Modify your maps outside of MTA Extra links Introduction A map-file! What is that? The name already gives away the definition. It is a file which contains a MTA map. The format makes maps portable, so that you can send them over to your friends. You can recognize map-files by the extension: .map Here is an example of a map: Syntax highlight When you open a map file in your text editor. The syntax highlight you should use is XML (HTML will work as well, but the semantic is different and could cause issues with auto complete features, that is if you have enabled those). Changing syntax highlight in for example Visual Studio Code. How to read a map file? Before we start, this is what we call a node: <tagName></tagName> If we take a closer look to the following map file: <map> <object id="object (bevgrnd03b_law) (1)" interior="0" collisions="true" alpha="255" doublesided="false" model="6094" scale="1" dimension="0" posX="635.234375" posY="-3827.2275390625" posZ="5" rotX="0" rotY="0" rotZ="0"></object> <object id="object (gaz9_law) (1)" interior="0" collisions="true" alpha="255" doublesided="false" model="6133" scale="1" dimension="0" posX="625.49114990234" posY="-3771.6955566406" posZ="11.479743003845" rotX="0" rotY="0" rotZ="0"></object> <object id="object (CE_grndPALCST03) (1)" interior="0" collisions="true" alpha="255" doublesided="false" model="13120" scale="1" dimension="0" posX="573.09802246094" posY="-3847.013671875" posZ="3.6442375183105" rotX="0" rotY="22" rotZ="352"></object> <object id="object (CE_grndPALCST03) (2)" interior="0" collisions="true" alpha="255" doublesided="false" model="13120" scale="1" dimension="0" posX="572.64624023438" posY="-3769.0698242188" posZ="4.9519920349121" rotX="0" rotY="21.99462890625" rotZ="343.24649047852"></object> <object id="object (CE_grndPALCST03) (3)" interior="0" collisions="true" alpha="255" doublesided="false" model="13120" scale="1" dimension="0" posX="669.66534423828" posY="-3856.0627441406" posZ="3.6442375183105" rotX="0" rotY="63.99462890625" rotZ="175.99389648438"></object> </map> > we see in there the map node: <map></map> When the map is loaded <map></map> will become our map element. And inside of the map node we see more nodes: <object id="object (bevgrnd03b_law) (1)" interior="0" collisions="true" alpha="255" doublesided="false" model="6094" scale="1" dimension="0" posX="635.234375" posY="-3827.2275390625" posZ="5" rotX="0" rotY="0" rotZ="0"></object> <object id="object (gaz9_law) (1)" interior="0" collisions="true" alpha="255" doublesided="false" model="6133" scale="1" dimension="0" posX="625.49114990234" posY="-3771.6955566406" posZ="11.479743003845" rotX="0" rotY="0" rotZ="0"></object> In this case these two nodes will become two in-game objects. Tagname <tagName></tagName> Each node in the map file will become an element in game. !important A node has a tag name. In this case I gave it the name: "tagName" This tag name specifies in MTA the element type. For example: If it has the name <object></object>, the element-type is an object. If it has the name <ped></ped>, the element-type is a ped. What if the tag name is not matching with one of these entities? (The list is not complete, the rest can be found in this class list, only if the XML syntax is implemented) Then the elements are considered custom elements. They are not visible in game. Custom elements Custom elements have their own purposes. You often see those custom elements used in game modes, for example stealth: <mercenaryspawn id="mercenaryspawn (1)" posX="635.58117675781" posY="-3770.458984375" posZ="18.97974395752" rotX="0" rotY="0" rotZ="0"></mercenaryspawn> The tagname for this node is "mercenaryspawn". The element that is produced after loading the map, is used as a spawnpoint for in a stealth map. Custom elements are invisible. Custom elements do have an orientation. The getElementsByType function can be used to get custom elements. Attributes Attributes are properties applied to a node. They are used to attach data to an element. Example: <entity name="IIYAMA" age="5" type="device" resolutionX="1920" resolutionY="1080" displaySize="31"></entity> An attribute exist out of two parts: <entity name="value"></entity> Name Value The name specifies if the node has the attribute. <entity name></entity> <entity age></entity> If the name is "name", then the node has the attribute "name". And if the name is "age", then the node has the attribute "age". The value will be used for the data for each attribute. <entity name="IIYAMA" age="5"></entity> Identifiers (ID) Every elements can have an identifier attribute, which is used to make it accessible for scripting functions. <tagName id="identifier"></tagName> The identifier of an element can be used in getElementByID to grant access to it. Even though identifiers normally should be unique for each individual, it is not the end of the world if they are not in MTA. They might also be considered as unnecessary for map files without scripts. Element specific attributes There are some attributes that do more than just applying properties. They will change the element appearance and orientation. For example this object: <object interior="0" collisions="true" alpha="255" doublesided="false" model="6094" scale="1" dimension="0" posX="635.234375" posY="-3827.2275390625" posZ="5" rotX="0" rotY="0" rotZ="0"></object> It will be created in interior 0. It will have collisions. (You can walk on it) Alpha. It's opacity is 100%. It is not doublesided. (When you stand inside of the object you can look through it. If doublesided is enabled the same colors/paint from the other side will be applied.) It's model is 6094. It is scaled 100%. (0.5 = 50%, 2 = 200%) Etc. If you want to know which attributes do have influence on the elements appearance and orientation, you have to look that up on the wiki. Here a small list of some those attributes: Broken map file, what to do? If your map file is broken. Step 1 Make a backup. + Always make a backup when re-editing your map. Settings can get lost! Step 2 The first thing you want to do, is just open it in a text-editor. Just give it a quick look and check for anything strange. The file could be empty or some strange values could be used. Do you want to know all the syntax rules (XML)? You can find a list right here: http://www.adobepress.com/articles/article.asp?p=1179145 Step 3 Remove the editor definition. This definition could cause problems in case of invalid characters. From: <map edf:definitions="COBDEF,editor_main"> <!-- Map elements --> </map> To: <map> <!-- Map elements --> </map> Step 4 If you can't find the problem, then you could validate the file. There are services out there that validate XML files. They can help you to find out where your file is broken. Validation tool by W3C: https://validator.w3.org/#validate_by_input Note: You need to do step 3 first. Else you can't parse the file. Step 5 In case of re-opening map files in the editor, but doesn't want to get open. Make a backup. Remove custom elements. See chapter How to read? Retry to open the file. Editor is bugged, what to do? Your editor could be bugged and you are not able to save the map or open a new one. The first thing you want to do is backup the editor_dump folder in your server. This folder is located between all your resources. server\mods\deathmatch\resources\editor_dump Rename the folder name. editor_dump > my_broken_map Stop the map editor. Check if there is a new editor_dump folder. If there is, rename that one as well. Start the map editor again. Modify your maps outside of MTA Sometimes you want to modify your maps without going back in to the editor. Your text-editor is in most cases the way to go. For example you want to move all objects to a new dimension. In Notepad++: Make a backup! (if you are new to this) Select in your file the part you want to replace: dimension="0" Commando: ctrl + H (replacement overlay) Fill in the replace field: dimension="1" Click on: replace ALL. Moving your map? There is a nice online tool that can help you with that: https://mtaclub.eu/converters Edit your map with the DOM (Document Object Model) Knowing JavaScript? Your browser inspector can be used to modify your maps at a higher level. (If you so desire) Extra links: Parent, child and how this is reflected in MTA https://wiki.multitheftauto.com/wiki/XML https://wiki.multitheftauto.com/wiki/Element_tree Elementdata <sync_map_element_data /> https://wiki.multitheftauto.com/wiki/Meta.xml Load maps (manually + including in meta.xml) https://wiki.multitheftauto.com/wiki/Meta.xml https://wiki.multitheftauto.com/wiki/LoadMapData Save maps https://wiki.multitheftauto.com/wiki/SaveMapData
-
I would like to know how I could get ElementData from server to client and vice versa -- server side marker = createMarker(0, 0, 1.5, "cylinder", 2, 255, 0, 0) setElementData(marker, "Item", 5) addEvent("GetItem", true) addEventHandler("GetItem", root, function() ClientMarker = marker ItemOfClientMarker = getElementData(marker, "Item") end) addEventHandler("onMarkerHit", marker, function(hp) triggerClientEvent("Element", resourceRoot) setElementData(hp, "ItemTwo", ElementVariable+getElementData(marker, "Item")) end) -- client side function dxDrawTextOnElement(TheElement,text,height,distance,R,G,B,alpha,size,font,...) local x, y, z = getElementPosition(TheElement) local x2, y2, z2 = getCameraMatrix() local distance = distance or 20 local height = height or 1 if (isLineOfSightClear(x, y, z+2, x2, y2, z2, ...)) then local sx, sy = getScreenFromWorldPosition(x, y, z+height) if(sx) and (sy) then local distanceBetweenPoints = getDistanceBetweenPoints3D(x, y, z, x2, y2, z2) if(distanceBetweenPoints < distance) then dxDrawText(text, sx+2, sy+2, sx, sy, tocolor(R or 255, G or 255, B or 255, alpha or 255), (size or 1)-(distanceBetweenPoints / distance), font or "arial", "center", "center") end end end end setElementData(getLocalPlayer(), "ItemTwo", 0) addEvent("Element", true) addEventHandler("Element", root, function() ElementVariable = getElementData(getLocalPlayer() , "ItemTwo") end) addEventHandler("onClientRender", root, function() triggerServerEvent("GetItem", root) dxDrawTextOnElement(ClientMarker, tostring(ItemOfClientMarker), 2, 50, 255, 255, 0, 3, "arial") end) Does it have a chance to work? because that's something I imagine...
-
-- server side cylinder = createMarker(-712.09100, 967.01917, 12.33547-1, "cylinder", 1.5, 255, 50, 0, 255) setElementData(cylinder, "BoxWithPoints", 0) function TIMER() if getElementData(cylinder, "BoxWithPoints") >= 8 then setElementData(cylinder, "BoxWithPoints", 8) else setElementData(cylinder, "BoxWithPoints", getElementData(cylinder, "BoxWithPoints")+1) end if isElementWithinMarker(source, cylinder) then triggerServerEvent("givePointsToServer", root) PointsServer = getElementData(source, "points") BoxPoints = getElementData(cylinder, "BoxWithPoints") setElementData(source, "points", PointsServer + BoxPoints) setElementData(cylinder, "BoxWithPoints", 0) end end setTimer(TIMER, 3000, 0) -- client side setElementData(getLocalPlayer(), "points", 0) addCommandHandler("ShowMyPoints", function() outputChatBox(tostring(getElementData(getLocalPlayer(), "points")), 255, 255, 0) end) addEvent("givePointsToServer", true) addEventHandler("givePointsToServer", root, function(player) local player = client setElementData(player, "points", getElementData(player, "points")) end) The problem is with the ruler where isElementWithinMarker (source, cylinder) shows me the error that argument 1 got nil, I don't know what to think about it because I checked meta.xml and server side and client side is the correct type, could someone explain why is this happening?
-
I would like to know something about ElementData, the most about these two commands {getElementData, setElementData}, could someone help me understand these two things. For example, write a guide that would help me a lot and give me a heart
-
Hi, I want to get list of visible elements (in my case pickups) to player in client-side code so I can loop through these elements. By visible, I mean literally player can "see" with their own eyes. You can also think it as loaded/rendered elements within given range. eg. element list of in 400.0 meters radius with player in center. I couldn't find proper function for it. Thank you for your further helps. Edit: these pickups are created server-side
-
Sooo, how can I use (I think for you well known) dxDrawTextOnElement() (client-side function, MTA Wiki site: [click]) in server-side scripts? I tried to export, plus this code: addEventHandler("onPlayerChat", resourceRoot, function(message, type_) if(type_ == 0) then call(resourceRoot, "dxDrawTextOnElement", source, message, 1, 20, 255, 255, 255, 255, 1, "default-bold") end end) (export in meta.xml for proof): <meta> <info author="VaporZ" name="VaporZ's utility [PL]" description="(desc)" type="script" version="1.0.1"/> <script src="cars.Lua" type="server"/> <script src="bank.Lua" type="server"/> <script src="utility.Lua" type="server"/> <script src="utility-c.Lua" type="client"/> <script src="testy.Lua" type="server"/> <file src="bank/accounts.xml"/> <file src="bank/atms.xml"/> <file src="cars/saved.xml"/> <file src="cars/shops.xml"/> <export function="dxDrawTextOnElement" type="client"/> </meta> But this don't work. Any ideas? Okay, I got further, but still it's not good. Look at this one (console error): ERROR: systems\utility.Lua:108: call: failed to call 'systems:dxDrawTextOnElement'
- 2 replies
-
- dxdrawtext
- dxdrawtextonelement
-
(and 7 more)
Tagged with:
-
Hello I'm just asking for function or way to get the local player in the Server-Side in the mod Please explain me all the possible ways to get the local player in the Server-Side in Easy , Clear and detailed way Thank you !
- 2 replies
-
- server side
- ped
-
(and 2 more)
Tagged with:
-
This is the code: local now = getTickCount() local x,y = interpolateBetween(one, one1, 0, two, two1, 0, (now - start) / ((start + 2500) - start), "Linear") local now2 = getTickCount() local x2,y2 = interpolateBetween(asd, asd1, 0, fgh, fgh1, 0, (now2 - start2) / ((start2 +2500) - start2), "Linear") if right == false then dxDrawRectangle(x, (sy_/2-20) * ym, 3* xm, 40 * ym,tocolor(255,255,255,255)) else dxDrawRectangle(x2, (sy_/2-20) * ym, 3* xm, 40 * ym,tocolor(255,255,255,255)) end -- if x >= 1063 and x <= 1073 and right == false then test = true elseif x >= 1089 and x <= 1099 and right == false then test = true elseif x >= 1158 and x <= 1172 and right == false then test = true elseif x >= 1212 and x <= 1232 and right == false then test = true elseif x >= 1276 and x <= 1293 and right == false then test = true elseif x >= 1423 and x <= 1433 and right == false then test = true elseif x >= 1476 and x <= 1487 and right == false then test = true else test = false end i want change the test to true when the rectangle is in the granted position, but how can i get the momentary position of the dx rectangle?
-
I want to render a rectangle to the cursor, but the rectangle too far from the cursor.. How to fix this in my code? code: local sx_, sy_ = 1920, 1080 local sx__, sy__ = guiGetScreenSize() local xm, ym = sx__/sx_, sy__/sy_ function render() local screenx, screeny = getCursorPosition() dxDrawRectangle((sx_-687) * xm * screenx, (sy_/2+70) * ym * screeny, 170 * xm, 25 * ym,tocolor(255,181,64,80)) end addEventHandler("onClientRender",root,render) the problem:
-
Olá Pessoal, como fazer para clicar em um veículo e receber no chat uma localização da roda dele? Já tenho a ideia de como usar o getVehicleComponentPosition (algo do lado do cliente), mas como identificar ou veicular o que estou tentando ver no OnElementClicked é do lado Server? function rodadireita (source) local vehicle = --Queria por aqui o veiculo que estou clicando x, y, z = getVehicleComponentPosition ( vehicle , "wheel_rf_dummy", "world") outputChatBox ( "Cordenadas:"..x..", "..y..", "..z, 255, 255, 255, true ) end
-
Quando utilizo: function aoSair(player) outputChatBox("| AVISO | você tem 30 segundos para voltar ao veículo.", player, 255,255,255, true) setTimer (function ( ) destroyElement(source) end, 30000, 1) end addEventHandler("onVehicleExit", getRootElement(), aoSair) o veículo não se destrói. Erro: Expected element at argument 1, got nil. Imagino que o erro aconteça porquê depois do jogador sair, source não é mais veículo, mas não sei como resolver. Outra coisa, estou usando esse método de destruir porque não consigo achar outra opção (sou novato em script). Queria que o veículo retornasse ao seu lugar padrão depois de 30 segundos: pegarEmprego = createMarker(1847.2760009766,-1873.8081054688,12.878125, "cylinder", 2, 255,255,255, 100) ped = createPed(20, 1847.7249755859,-1873.9541015625,13.578125, 90, false) setElementFrozen(ped, true) function mule(BVOMule) car1 = createVehicle(414, 1780.3957519531,-1889.2502441406,13.38960647583,-0, 0, 268.3544921875) car2 = createVehicle(414, 1780.3957519531,-1889.2502441406-5,13.38960647583,-0, 0, 268.3544921875) car3 = createVehicle(414, 1780.3957519531,-1889.2502441406-10,13.38960647583,-0, 0, 268.3544921875) end addEventHandler("onResourceStart", resourceRoot, mule) function aoEntrar(player) skin = getElementModel(player) gEM = getElementModel(source) if ( gEM == 414 ) then if skin == 20 then if getElementData(player, "Entregador") then else outputChatBox("| SERVIÇO | você precisa estar com a skin.", player) cancelEvent() end else end end end addEventHandler( "onVehicleStartEnter", getRootElement(), aoEntrar ) function msgEmprego(hitElement) if getElementType(hitElement) == "player" then if getElementData(hitElement, "Entregador") == false then outputChatBox("#ffffff| EMPREGO | digite #ff0000/conversar #ffffffpara conversar com o entregador.", hitElement, 255,255,25, true) end end end addEventHandler("onMarkerHit", pegarEmprego, msgEmprego) function aoSair(player) outputChatBox("| AVISO | você tem 30 segundos para voltar ao veículo.", player, 255,255,255, true) setTimer (function ( ) destroyElement(source) end, 1000, 1) end addEventHandler("onVehicleExit", getRootElement(), aoSair) function conversarComEntregador(playerSource) if isElementWithinMarker(playerSource, pegarEmprego) then if getElementModel(playerSource) == 20 then outputChatBox("#ffffffENTREGADOR: você já está com a roupa de entregador, pode trabalhar.", playerSource, 255,255,255, true) else outputChatBox("#ffffffENTREGADOR: lhe dei a roupa de entregador, pode ir trabalhar.", playerSource, 255,255,255, true) setPlayerSkin(playerSource, 20) setElementData(playerSource, "Entregador", true) end end end addCommandHandler("conversar", conversarComEntregador)
-
local vehicleElements = {} allVeh = {510} local pickupTable = { {1086.1798095703,-1801.6898193359,13.601690292358}, } function createPickupOnServerStart () for i, cor in ipairs (pickupTable) do local pickup = createPickup(cor[1], cor[2], cor[3], 3, 1274, 0) setElementData(pickup, "pickupArenda", true) end end addEventHandler("onResourceStart", getResourceRootElement( getThisResource() ), createPickupOnServerStart) function onResourceStop (veh) if isTimer(vehicleElements[veh]) then killTimer(vehicleElements[veh]) end --vehicleElements[veh] = destroyElement(veh) end addEventHandler("onResourceStop", getResourceRootElement( getThisResource() ), onResourceStop) function onPickupHit (hit) if not getElementData(source, "pickupArenda") or getElementType(hit) ~= "player" and not isPedInVehicle(hit) then return end triggerClientEvent(hit, "changeArendaMenuState", hit) end addEventHandler("onPickupHit", getRootElement(), onPickupHit) function onPlayerUseCustomPickup() if getPlayerMoney(source) >= 500 then local x, y, z = getElementPosition(source) local veh = createVehicle(allVeh[math.random(1,#allVeh)], x, y, z) setElementData(veh, "starter_pack", true) setElementData(veh, "owner", source) warpPedIntoVehicle(source, veh) takePlayerMoney(source, 500) outputChatBox("Bicicleta alugada, bom uso!.", source, 0, 255, 0) else outputChatBox("Para alugar uma bicicleta você precisa de >> 500 $$ <<.", source, 255, 0, 0) end end addEvent("onPlayerUseCustomPickup", true) addEventHandler("onPlayerUseCustomPickup", getRootElement(), onPlayerUseCustomPickup) function onPlayerVehicleEnter (veh, seat) if getElementData(veh, "starter_pack") == true and seat == 0 then if isTimer(vehicleElements[veh]) then killTimer(vehicleElements[veh]) end end end addEventHandler("onPlayerVehicleEnter", getRootElement(), onPlayerVehicleEnter) function onPlayerVehicleExit (veh, seat) if getElementData(veh, "starter_pack") == true and seat == 0 then if isTimer(vehicleElements[veh]) then killTimer(vehicleElements[veh]) end vehicleElements[veh] = setTimer(destroyElement, 300000, 1, veh) end end addEventHandler("onPlayerVehicleExit", getRootElement(), onPlayerVehicleExit) function onVehicleStartEnter (player, seat) if getElementData(source, "starter_pack") == true and seat == 0 then local owner = getElementData(source, "owner") if owner ~= player then cancelEvent() outputChatBox("Você não é dono desta bicicleta, alugue sua bike para poder usá-la.", player, 255, 0, 0) end end end addEventHandler("onVehicleStartEnter", getRootElement(), onVehicleStartEnter) This is the script to rent bike to a player. The bike is destroyed after 5 minutes, it's ok. But i need to do something that when player is arrasted, die, logout, exit of game the bike be destroyed too. function DestroyVeiculo (veh, seat) if getElementData(veh, "starter_pack") == true and seat == 0 then if isTimer(vehicleElements[veh]) then killTimer(vehicleElements[veh]) end --vehicleElements[veh] = setTimer(destroyElement, 300000, 1, veh) vehicleElements[veh] = destroyElement(veh) end end addEventHandler ("onPlayerLogout", getRootElement(), DestroyVeiculo) addEventHandler ("onPlayerQuit", getRootElement(), DestroyVeiculo) addEventHandler ("onPlayerWasted", getRootElement(), DestroyVeiculo) addEventHandler ("onPlayerBan", getRootElement(), DestroyVeiculo) addEventHandler ("onPlayerDie", getRootElement(), DestroyVeiculo) I tried to do it the way above, but not sucess. Someone can help me ? Thanks!
-
This is in 1440x900 (in my resolution): This is in 1920x1080: I calculate the dx elements position with this: local sx_, sy_ = guiGetScreenSize() local sx, sy = sx_/1440, sy_/900 And i draw dx elements like this: dxDrawRectangle(sx / 2 + 290, sy / 2 +240,850,65,tocolor(0,0,0,200)) What wrong??? How can i fix this problem??
-
Events tutorial The reason why I created this topic, is that a lot of people are struckeling with them. In this tutorial I will only discus the very basic of them. If you want more, then there is a list of links at the end with more information. If I made any mistakes in the code, please let me know because I am not going to test every part. What are events? (basic description) Events are something custom added by MTA to make it easier to bring scripting(Lua) closer to our game. If we do not have events, then the only thing we can do is give instructions to our game. But our code will never detect changes in our game. The same question again: "So what are the events?" Events are a way to communicate changes in our game to our scripts (or from our scripts). So for example my little ped(cat) gets ran over by a car. Then I really want to know about that, don't I? When an event activates, I describe this as: triggered (from the word trigger) Full wiki information can be found here: Event_system Before we can use events, what do we need to know? There are two things we need to know: The reason why it is triggered. <What happens?/when something happens?> In MTA this is the eventName of the event. (Example: you <ran over> my ped) Who is the one using the event? The event system in MTA is using elements as base (baseElement). This makes it easier to combine the what/when happens? with the one who is related to it. In MTA this is the source of the event. (Example: you ran over my <ped>) Trigger an event A scripting example: (this is not an official event) local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped triggerEvent("onPedRanOver", ped) In this example, a custom event gets triggered to tell you that my new created ped has been ranOver. The eventName is "onPedRanOver" and the baseElement is ped. local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) In this SERVERSIDE example, I am also adding an extra argument drunkDriver to use the player that ran over the ped. This is not required, but it makes it more complete. See syntax. Syntax bool triggerEvent ( string eventName, element baseElement, [ var argument1, ... ] ) TriggerEvent Receiving triggers Receiving triggers is a bit more complicated, because there are a lot of options for it. You can receive events by listening to them. It is like: You know that something is going to happen but you do not know when. The first step that you have to take is related to this question: "Do I want to receive a custom or not custom event?" Custom events are self created events. They only exist when a scripter makes them. Two lists of NOT CUSTOM EVENTS but default MTA events, serverside and clientside: Server_Scripting_Events Client_Scripting_Events Do I want to receive a CUSTOM event? In case of a custom event, you have to register/enable it first. If you do not enable it and trigger it, you will receive a warning/error about that in your debug console. Syntax bool addEvent ( string eventName [, bool allowRemoteTrigger = false ] ) AddEvent The example below, shows you how to enable a custom event only for trigger events within the same server/client side. addEvent("eventName") -- Is the same as: addEvent("eventName", false) If you put the second argument to false or not fill it in, this means that you can't communicate from the other server/client-side. This option is most likely used for security reasons. Some events shouldn't be able to trigger by the other side For example, worst case scenario: (remote events enabled for a default MTA event) Serverside code: addEvent("onPlayerWasted", true) Clientside code: triggerServerEvent("onPlayerWasted", player, 0, localPlayer, 0, 9, false) OnPlayerWasted If this event is enabled for remote trigger events, then it might be possible to cheating kills/deaths score. Of course, it is not likely that players can run their own clientside code, but it is not impossible in case of not trust able community resources. Enable a custom event for trigger events that crossing sides (From clientside to serverside. From serverside to clientside). addEvent("eventName", true) This event can now be used by remote trigger event functions. See list: Client to server TriggerClientEvent TriggerLatentClientEvent Server to client TriggerServerEvent TriggerLatentServerEvent Enable the event from our previous example: addEvent("onPedRanOver", false) local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) If you do not use cross triggering, then I recommend to use the addEvent function in the same resource as where you are going to trigger from. This makes sure that the event is already added and that you will never receive this kind of error/warning "Event isn't added". If you put it in another resource which hasn't started yet, then after triggering you would still receive that error/warning. Start listening The next step is to add the addEventHandler. This function is used to listen to events. When an event is triggered, this handler(addEventHandler) will call the function you have attached to it, in MTA this function is called the handlerFunction. Syntax bool addEventHandler ( string eventName, element attachedTo, function handlerFunction [, bool getPropagated = true, string priority = "normal" ] ) AddEventHandler Resource 1 addEvent("onPedRanOver", false) local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) Resource 2 function handlerFunction () end addEventHandler("onPedRanOver", root, handlerFunction) The first 3 arguments, the require ones: eventName attachedTo handlerFunction Making sure that the addEventHandler options are correct set-up. Resource 1 addEvent("onPedRanOver", false) local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) Resource 2 function handlerFunction () end addEventHandler("onPedRanOver", root, handlerFunction) There are two conditions for an eventHandler to call the handlerFunction. 1. The event has to be exactly the same. In this case the event "onPedRanOver" is the same in both resources. 2. In both functions, triggerEvent and addEventHandler is an element being used. This element has to be exactly the same. (from where you trigger as well as where you receive) <OR> The triggered element from resource 1, has to be a CHILD of the element in resource 2. The root element is the very top layer of the MTA element structure. It will accept all elements you want to use for your events. See the element tree: If you do not understand the element tree please read this page: Element_tree Source variable The source of an event is the element that triggers the event. This variable isn't passed as an parameter, but it is predefined. This means that it is already created before hand. Some predefined variables do only exist under special conditions. The source variable is one of those, it is a hidden and local variable which is only available when a function is called by an event. List of predefined variables. addEvent("onPedRanOver", false) -- local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) function handlerFunction (drunkDriver) iprint(source) -- ped element end addEventHandler("onPedRanOver", resourceRoot, handlerFunction) In this example the ped is the source. See how those two code blocks are connected: addEvent("onPedRanOver", false) -- local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) function handlerFunction (drunkDriver) iprint(source) -- ped element end addEventHandler("onPedRanOver", resourceRoot , handlerFunction) resourceRoot In some examples, you see people use the resourceRoot instead of the root element for their addEventHandlers. The resourceRoot is an element created by a resource. This element holds all elements of that resource as (in)direct children. In the example above, the resourceRoot as baseElement will not work, because there are two resources. Each resource has it's own resourceRoot element. The resourceRoot is accessible with the same keyword: resourceRoot, but if you were to inspect the element in multiple resources, then the user data (element identifier) value is not the same. outputChatBox(inspect(resourceRoot)) If we were to put everything in one resource, then it would work: ? addEvent("onPedRanOver", false) -- function handlerFunction () end addEventHandler("onPedRanOver", resourceRoot, handlerFunction) -- local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) In case of remote triggering, the resourceRoot in serverside and clientside is considered the same.(As long as they are part of the same resource) Why/when would we use resourceRoot? 1. Limit eventHandlers to the resource elements If you have 1000 markers in your server. One of the resources is for example a trucker mission, where you can get money by hitting markers. The resourceRoot element will make sure that the onMarkerHit event will only trigger for markers created by that resource. addEventHandler("onMarkerHit", resourceRoot, function () -- source element is the marker end) OnMarkerHit 2. Another benefit is that you are able to re-use the same eventNames. Resource 1 addEvent("onPedRanOver", false) function handlerFunction () end addEventHandler("onPedRanOver", resourceRoot, handlerFunction) -- local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) Resource 2 addEvent("onPedRanOver", false) function handlerFunction () end addEventHandler("onPedRanOver", resourceRoot, handlerFunction) -- local ped = createPed( 120, 5540.6654, 1020.55122, 1240.545 ) -- my ped local drunkDriver = getRandomPlayer() triggerEvent("onPedRanOver", ped, drunkDriver) These two resources do use the same event, but will not trigger each other their addEventHandlers. Warning: If root was used, then they will!!!! ;@ Lets cross triggering with resourceRoot! Clientside triggerServerEvent("example", resourceRoot) Serverside addEvent("example", true) -- second argument is true! cross triggering enabled! addEventHandler("example", resourceRoot, function () end) getPropagated In this bigger example we will be talking about the option getPropagated. If this option is disabled, it will not detect children any more. Keep reading! After that start code scanning from A, to B and then to C. Syntax addEventHandler bool addEventHandler ( string eventName, element attachedTo, function handlerFunction [, bool getPropagated = true, string priority = "normal" ] ) Example: Clientside -- A triggerServerEvent("onClientPlayerLoaded", resourceRoot) -- trigger an event to serverside --------------------------------------- -- C addEvent("onResponseServer", true) -- first listener addEventHandler("onResponseServer", resourceRoot, function () outputChatBox("getPropagated enabled") end, true) -- getPropagated true by default. -- second listener addEventHandler("onResponseServer", resourceRoot, function () outputChatBox("getPropagated disabled") end, false) -- getPropagated is false. Serverside -- B addEvent("onClientPlayerLoaded", true) -- second argument is true! cross triggering enabled! addEventHandler("onClientPlayerLoaded", resourceRoot, function () --[[ client is a predefined variable, which represents the client/player that communicates with the server More information about predefined variables: https://forum.multitheftauto.com/topic/33407-list-of-predefined-variables/ ]] triggerClientEvent(client, "onResponseServer", resourceRoot) -- first trigger event local element = createElement("randomElement") -- making a randomElement triggerClientEvent(client, "onResponseServer", element) -- second trigger event end) How does this this code works? A. When a client his code has been started, it will execute a triggerServerEvent. (It doesn't wait for any other clientside files to be loaded) B. The server receives the event. And sends two triggerClientEvents back: The first one is using the resourceRoot as baseElement. The second one is using a randomElement as baseElement. Both are using the event "onResponseServer" C. There are two addEventHandlers listening to the event: "onResponseServer" The first one is using getPropagated and the second one is not using getPropagated. The randomElement that is created, is by default an indirect child of the resourceRoot of the same resource. What will happen? When firing the first trigger event, both listeners will call their handlerFunction. But when firing the second trigger event, only the first listener will call it's handlerFunction. The randomElement is an indirect child of resourceRoot, but because getPropagated is disabled it will not call it's handlerFunction. Other tutorials related to this one: See also this tutorial about deeper limiting event ranges within your resource and reducing addEventHandlers https://forum.multitheftauto.com/topic/100069-tut-addeventhandler-on-a-group-of-elements-small-tutorial/ More information Full wiki information: Event_system A list of more information about triggering events: (Client to client / server to server) TriggerEvent Client to server TriggerClientEvent TriggerLatentClientEvent Server to client TriggerServerEvent TriggerLatentServerEvent A list of more information about receiving events: AddEvent AddEventHandler RemoveEventHandler Two lists of MTA events, serverside and clientside: (warning: not custom events) Server_Scripting_Events Client_Scripting_Events Cancel events CancelEvent WasEventCancelled (warning: custom events ONLY) GetCancelReason (Server only) Cancel latent events and their status GetLatentEventHandles CancelLatentEvent GetLatentEventStatus
-
Hi! My problem is that on the client side the object is seen by only one person who has tried it out and I want everyone to see the object on the server. Client Side: local rx,ry,rz = getElementPosition(getElementData(localPlayer,"cage")) local cx,cy,cz = getVehicleComponentPosition(getElementData(localPlayer,"cage"),"static_p01") local x,y,z = rx + cx, ry + cy+3, 0.8 local a,b = getScreenFromWorldPosition(x,y,z) local magnet = createObject(1301, rx-0.6, ry-5, rz+4, 0, 0, 0) setObjectScale(magnet, 0.3) setElementCollisionsEnabled(magnet, false) moveObject(magnet, 1000*60*1, rx-0.6, ry-5, rz-50) setTimer(function() destroyElement(magnet) local magnet2 = createObject(1301, rx-0.6, ry-5, rz-50, 0, 0, 0) setObjectScale(magnet2, 0.3) setElementCollisionsEnabled(magnet2, false) moveObject(magnet2, 1000*60*1, rx-0.6, ry-5, rz+4) local crate = createObject(1224, rx-0.6, ry-5, rz-50.3, 0, 0, 0) moveObject(crate, 1000*60*1, rx-0.6, ry-5, rz+3.3) setElementCollisionsEnabled(crate, false) setTimer(function() usedMagnet = false end, 1000*60*1, 1) end, 1000*60*1, 1) In principle I should only use this server site: setElementVisibleTo
-
Hello. Is there any difference between using the entity's name instead of a custom name in a variable? local vehicle = createVehicle (...) or local theVehicle = createVehicle (...) Why does the Wiki always avoid using the entity's name as variable? If I use the entity's name instead of a custom name, is this a bad programming method? PS: I am not using OOP.
-
is it possible to get an element from a table by type , for example when i get attached elements to my car , i attached a sound element to my car. but get attached elements returns all the attached elements in to a table ( as i know) , is there a way to get the sound element from the table ???
-
I want create a bank-system. But i don't know, how to begin the script. Can i make it possible this with setElementData, and getElementDatas? sorry for my bad english
-
I don't understand this Error = [Expected bool at argument 2, got none]... how to use function clientSide setElementFrozen ? --client side script function FPlayer () setElementFrozen (this, true) --This is how this method? end
-
I created "RepairMarkers" and when player hits any of them, their car's health must to increase. But I don't know, how to check every marker here: if isElementWithinMarker( getLocalPlayer(), RepairMarker[1] ) then Instead of "RepairMarker[1]" I want to check every marker Source: function CreateMarkers( thePlayer, seat ) if thePlayer == getLocalPlayer() and seat == 0 then BaysideRM = createMarker( -2259.027, 2386.142, 3.897, "cylinder", 3, 233, 96, 21, 100 ) Area69SkyRM = createMarker( 307.876, 2051.747, 16.587, "cylinder", 5, 233, 96, 21, 100 ) RepairMarker = { BaysideRM, Area69SkyRM } end end function RepairVehicle( hitPlayer, matchingDimension ) if hitPlayer == getLocalPlayer() and matchingDimension == true then local theVehicle = getPedOccupiedVehicle( getLocalPlayer() ) local VehicleHealth = getElementHealth( theVehicle ) local RepairHealth = math.floor( VehicleHealth ) function Repair() if isElementWithinMarker( getLocalPlayer(), RepairMarker[1] ) then if RepairHealth < 1000 then RepairHealth = RepairHealth + 1 setElementHealth( theVehicle, RepairHealth ) else fixVehicle( theVehicle ) playSFX( "script", 11, 1, false ) end else killTimer( RepairTimer ) end end if VehicleHealth < 1000 then RepairTimer = setTimer( Repair, 100, 1001 - math.floor( VehicleHealth ) ) end end end function DestroyMarkers( thePlayer, seat ) if thePlayer == getLocalPlayer() and seat == 0 then for i, v in ipairs( RepairMarker ) do destroyElement( RepairMarker[i] ) end end end addEventHandler( "onClientMarkerHit", getRootElement(), RepairVehicle ) addEventHandler( "onClientVehicleEnter", getRootElement(), CreateMarkers ) addEventHandler( "onClientVehicleExit", getRootElement(), DestroyMarkers )
-
function test() setElementPosition ( mark, pos.x,pos.y,pos.z ) end addEventHandler("onMarkerHit", mark, test) this is the code. if i hit the marker, the element position change for all players.. how to fix this? sorry for my bad english.
-
hello all i have scripted a command for warehouse but the problem is i want a teleport button for knife arena in my exist guiwindow! client.lua : addCommandHandler( "knife", function( player ) setElementInterior( player, 1, 1401.3, -15, 1000.8 ) -- teleport player to the centre of SA end ) for gui code i dont want to paste here come pm for cilent code of guiwindow,button why i dont want to paste here is members can easy clown my scripts by using my cilentcode!! please help me guys screenshoot ; https://imgur.com/WgJR2Qi