Recommended Posts

My problem is that it's not storing the data in the local mg = {}. The debugscript says: bad argumentum @ 'setElememtData' expected element at argument 1, got table

Thanks in advance!

function mg_pos()
createMG(-2861.9, 19.4, 15.83, 0, 0, 188)
end
addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), mg_pos)



--MG 1
local mg = {}

function createMG(posX,posY,posZ,rotX,rotY,rotZ)
  local weapon_base = createObject ( 1897, posX,posY,posZ,rotX,rotY,rotZ)
  local weapon = createObject ( 356, 0,0,0)  
  attachElements ( weapon, weapon_base, -0.55, 0, 0.75, 0, 0, 0)
  setElementData ( mg, "weapon_base", weapon_base)
  setElementData ( mg, "weapon", weapon) 
end

Share this post


Link to post

What are you trying to do? Why do you want to set the element data of a LUA table? 

Share this post


Link to post

I try to get the element data with getElementData(mg, "weapon_base") but its not getting the element data.

Share this post


Link to post
1 hour ago, Xwad said:

I try to get the element data with getElementData(mg, "weapon_base") but its not getting the element data.

mg is not an element, mg is a lua table. You can't set an element data of a lua table. Post your full code so we can help.

Share this post


Link to post

You should either use the table properly with table.insert or simply use MTA function createElement and then attach data to it. Your choice, both work just fine although I'd go with tables because few ticks matter to me (for performance).

Share this post


Link to post

ok, now i edited my script a littlebit. i removed the table and added a local element, but its still not sotring the data :/

server

function mg_pos()
createMG(-2861.9, 19.4, 15.83, 0, 0, 188)
end
addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), mg_pos)


function createMG(posX,posY,posZ,rotX,rotY,rotZ)
  local mg = createObject ( 356, 0,0,0)  -- this is where the data will be stored (new line)
  local weapon_base = createObject ( 1897, posX,posY,posZ,rotX,rotY,rotZ)
  local weapon = createObject ( 356, 0,0,0)  
  attachElements ( weapon, weapon_base, -0.55, 0, 0.75, 0, 0, 0)
  setElementData ( mg, "weapon_base", weapon_base)
  setElementData ( mg, "weapon", weapon) 
  setElementAlpha(weapon_base, 0)
  
  setTimer ( event, 500, 1 )
end

function event()
local weapon = getElementData ( mg, "weapon" )
triggerClientEvent("createRealWeapon", root, weapon, weapon2)
end

and client

function create_client(weapon)
local x,y,z = getElementPosition ( weapon )
local weapon_c = createWeapon("minigun", x,y,z)
attachElements ( weapon_c, weapon, 1.1, -0.12, 0.152, -6, 3, -2 )
setElementCollisionsEnabled( weapon_c, false )
setWeaponState ( weapon_c, "ready" )
setElementAlpha ( weapon_c, 255 )
setWeaponFiringRate ( weapon_c , 60 )
setElementParent( weapon_c, weapon )
setWeaponProperty( weapon_c, "damage", 0)
setWeaponProperty ( weapon_c, "fire_rotation", 0, -27, -5.5 )
setWeaponProperty ( weapon_c, "weapon_range", 10000 )
setWeaponProperty ( weapon_c, "target_range", 10000 )
setWeaponClipAmmo( weapon_c, 250 )
setElementData ( weapon_c, "owner", player )
setElementData ( localPlayer, "gun", weapon_c )
end
addEvent("createRealWeapon", true)
addEventHandler("createRealWeapon",root, create_client)



 

Share this post


Link to post

What do you mean by not "sotring" the data? (sorting? but that wouldn't make sense either..) What is happening/not happening, any errors in debugscript? And you don't have to create an actual object to save the element data, you can create a static "element", with createElement, although using table would be just fine, you just didn't know how to use them. To insert data into a table use:

local myTable = {}


table.insert(myTable, "myString") --> [1]
table.insert(myTable, {"mystring2", "mystring3"}) -- [2][1] AND [2][2] because I added a table inside the table using { }
myTable["myKey"] = "myString4" -- ["myKey"] here you access the data with a string key, not a number as in the previous 2

outputChatBox(myTable[1])
outputChatBox(myTable[2][1])
outputChatBox(myTable[2][2])
outputChatBox(myTable["myKey"])

--[[Output:
myString
myString2
myString3
myString4

Table looks like this after insert:

local myTable = {
	"myString",
	{ "myString2", "myString3" },
	["myKey"] = "myString4"
}

]]

 

Edited by pa3ck

Share this post


Link to post

No need to setElementData

Server:

function mg_pos()
	createMG(-2861.9, 19.4, 15.83, 0, 0, 188)
end
addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), mg_pos)


function createMG(posX,posY,posZ,rotX,rotY,rotZ)
	local mg = createObject ( 356, 0,0,0)  -- this is where the data will be stored (new line)
	local weapon_base = createObject ( 1897, posX,posY,posZ,rotX,rotY,rotZ)
	local weapon = createObject ( 356, 0,0,0)  
	attachElements ( weapon, weapon_base, -0.55, 0, 0.75, 0, 0, 0)
	setElementAlpha(weapon_base, 0)
	setTimer ( event, 500, 1, weapon_base )
end

function event(weapon_base)
	triggerClientEvent("createRealWeapon", root, weapon_base)
end

 

Share this post


Link to post

yes i need the setElementData, because there are other functions that cant get the data without the setElementData. When i remove "local" from local mg = createObject ( 356, 0,0,0then its working, but i want to use it in local

Share this post


Link to post
16 minutes ago, Xwad said:

yes i need the setElementData, because there are other functions that cant get the data without the setElementData. When i remove "local" from local mg = createObject ( 356, 0,0,0then its working, but i want to use it in local

That's not how it works.. you want to keep it local but you expect other functions to be able to access it? You can keep it local, but pass the variable to the other function(s) so they have access to it, try this and let us know what happens:

function mg_pos()
createMG(-2861.9, 19.4, 15.83, 0, 0, 188)
end
addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), mg_pos)


function createMG(posX,posY,posZ,rotX,rotY,rotZ)
  local mg = createObject ( 356, 0,0,0)  -- this is where the data will be stored (new line)
  local weapon_base = createObject ( 1897, posX,posY,posZ,rotX,rotY,rotZ)
  local weapon = createObject ( 356, 0,0,0)  
  attachElements ( weapon, weapon_base, -0.55, 0, 0.75, 0, 0, 0)
  setElementData ( mg, "weapon_base", weapon_base)
  setElementData ( mg, "weapon", weapon) 
  setElementAlpha(weapon_base, 0)
  
  setTimer ( event, 500, 1, mg )
end

function event(mg)
local weapon = getElementData ( mg, "weapon" )
triggerClientEvent("createRealWeapon", root, weapon, weapon2)
end

 

Share this post


Link to post

Well, i can't explain myself eneugh, so here is my full script. This script creates a machine gun on the map, in which you can enter and shoot. What i want to do is duplicate the machine gun. I mean creating a second machine gun. More precisely: If i put one more "createMG" function after line 2 then it will create a new machinegun.

 

  1. function mg_pos()
  2. createMG(-2861.9, 19.4, 15.83, 0, 0, 188)
  3. end
  4. addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), mg_pos)

 

I tried to do it in a lot of ways, but always when i tested it, it had a lot of sync bugs. I ask you guys for help to fix this script. I am creating this script for the community.

http://www.mediafire.com/file/3oud140w15dims4/mg.rar

 

 

Share this post


Link to post

Do you mean like this?

-- create table contain the position and rotation of machine gun
machineguns = {
	-- x, y, z, rotx, roty, rotz
	{-2861.9, 19.4, 15.83, 0, 0, 188},
	{-2864.9, 19.4, 15.83, 0, 0, 188},
	-- and more you can add it..
}

function mg_pos()
	for _, v in pairs(machineguns) do
		createMG(v[1], v[2], v[3], v[4], v[5], v[6])
	end
end
addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), mg_pos)

 

Share this post


Link to post

yeah, it can also be like this, but this will not fix the bugs:/

Share this post


Link to post

Try doing this and tell me if it works:

 

function mg_pos()
createMG(-2861.9, 19.4, 15.83, 0, 0, 188)
end
addEventHandler ( "onResourceStart", resourceRoot, mg_pos)


local mgs = { }
local weapon_bases = { }
local weapons = { }
function createMG(posX,posY,posZ,rotX,rotY,rotZ)
  local number = #mgs+1
  mgs[number] = createObject ( 356, 0,0,0)  -- this is where the data will be stored (new line)
  weapon_bases[number] = createObject ( 1897, posX,posY,posZ,rotX,rotY,rotZ)
  weapons[number] = createObject ( 356, 0,0,0)  
  attachElements ( weapons[number], weapon_bases[number], -0.55, 0, 0.75, 0, 0, 0)
  setElementData ( mgs[number], "weapon_base", weapon_bases[number])
  setElementData ( mgs[number], "weapon", weapons[number]) 
  setElementAlpha(weapon_bases[number], 0)
  
  setTimer ( event, 500, 1, mgs[number] )
end

function event(mg)
local weapon = getElementData ( mg, "weapon" )
triggerClientEvent("createRealWeapon", root, weapon, weapon2)
end

 

Share this post


Link to post

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.