Turbesz Posted November 28, 2020 Posted November 28, 2020 I want get data from table, to use a object (move, etc.), but i don't know how to do this. I tried this method: local targy = {} local cp = {} function lerakja(thePlayer) local x, y, z = getElementPosition(thePlayer) targy[thePlayer] = createObject(2167, x+1, y+1, z-1) setElementData(targy[thePlayer], "owner", thePlayer) end addCommandHandler("obj",lerakja) function clickfunc ( button, state, player ) if button == "left" and state == "down" then if getElementType ( targy[player] ) == "object" and getElementData(targy[player],"owner") == player then local x, y, z = getElementPosition(targy[player]) outputChatBox("you are use now this object: ID: "..#targy[player]) cp[player]= createMarker(x, y-0.2, z+2.5, "arrow", 0.7, 255, 181, 64, 250) end end end addEventHandler ( "onElementClicked", root, clickfunc ) The object spawn fine, and the cp spawn fine too above my object after i click. But if i spawn another object, the cp spawn above my object too again. This is fine, but i want to get data from that object which one I clicked to remove the previous cp from the previous object. How can i get data from my table to know how to do it this? (Sorry for my very bad english:/)
Tekken Posted November 28, 2020 Posted November 28, 2020 Just use if targy[player] == source then —do something.. end onElementClicked
Turbesz Posted November 28, 2020 Author Posted November 28, 2020 11 minutes ago, Tekken said: Just use if targy[player] == source then —do something.. end onElementClicked i tried this, but if i create a new object, the old one does not respond to click
Tekken Posted November 28, 2020 Posted November 28, 2020 Well count the id’s targy[player][ tonumber(#targy[player]) + 1 ] = element; tonumber not needed normally but just to be sure
Turbesz Posted November 28, 2020 Author Posted November 28, 2020 3 minutes ago, Tekken said: Well count the id’s targy[player][ tonumber(#targy[player]) + 1 ] = element; tonumber not needed normally but just to be sure attempt to get length of field '?' (a nil value) targy[player][tonumber(#targy[player]) + 1] = createObject(2167, x+1, y+1, z-1) if i add this line without tonumber, i got the same error
Tekken Posted November 28, 2020 Posted November 28, 2020 Replace #targy[player] to #targy[player] or 0
Turbesz Posted November 28, 2020 Author Posted November 28, 2020 4 minutes ago, Tekken said: Replace #targy[player] to #targy[player] or 0 I got the same error
Tekken Posted November 28, 2020 Posted November 28, 2020 You define the table with the player first targy[player] = {}
Turbesz Posted November 28, 2020 Author Posted November 28, 2020 2 minutes ago, Tekken said: You define the table with the player first targy[player] = {} i got the same error with this, but if it had worked, then the table would be emptied, and if i try change "local targy = {}" to "local targy[source] = {}" out of the function, i got another error
Tekken Posted November 28, 2020 Posted November 28, 2020 Wait just do table[player][1] = object And create a table.count function like this function tablecount(table) if type(table) == "table" then local count = 0; for i,v in ipairs(table) do count = count +1; end return count; end return false; end and do table[player][(tablecount(table[player]) or 0) + 1]
Turbesz Posted November 28, 2020 Author Posted November 28, 2020 6 minutes ago, Tekken said: Wait just do table[player][1] = object Like this? local targy = {} function lerakja(thePlayer) local x, y, z = getElementPosition(thePlayer) table[thePlayer] = createObject(2167, x+1, y+1, z-1) setElementData(table[thePlayer], "owner", thePlayer) end addCommandHandler("obj",lerakja) function clickfunc ( button, state, player ) if button == "left" and state == "down" then if table[player][1] == source then local x, y, z = getElementPosition(table[player][1]) outputChatBox(x.." | "..y.." | "..z) end end end addEventHandler ( "onElementClicked", root, clickfunc )
Turbesz Posted November 28, 2020 Author Posted November 28, 2020 1 minute ago, Tekken said: You should replace table with targy uf, my bad but i got this error now: attempt to index field '?' (a userdata value)
Tekken Posted November 28, 2020 Posted November 28, 2020 Just realized a thing function lerakja(thePlayer) local x, y, z = getElementPosition(thePlayer) targy = createObject(2167, x+1, y+1, z-1) setElementData(targy, "owner", thePlayer) addEventHandler ( "onElementClicked", targy, clickfunc ) end addCommandHandler("obj",lerakja) function clickfunc ( button, state, player ) if button == "left" and state == "down" then if getElementType (source ) == "object" and getElementData(source,"owner") == player then local x, y, z = getElementPosition(source) local model = getElementModel(source) outputChatBox("you are use now this object: ID: "..model) cp = createMarker(x, y-0.2, z+2.5, "arrow", 0.7, 255, 181, 64, 250) end end end This should work May I know what you are exactly trying to archive? Didn’t quite understand yet
Turbesz Posted November 28, 2020 Author Posted November 28, 2020 6 minutes ago, Tekken said: Just realized a thing function lerakja(thePlayer) local x, y, z = getElementPosition(thePlayer) targy = createObject(2167, x+1, y+1, z-1) setElementData(targy, "owner", thePlayer) addEventHandler ( "onElementClicked", targy, clickfunc ) end addCommandHandler("obj",lerakja) function clickfunc ( button, state, player ) if button == "left" and state == "down" then if getElementType (source ) == "object" and getElementData(source,"owner") == player then local x, y, z = getElementPosition(source) local model = getElementModel(source) outputChatBox("you are use now this object: ID: "..model) cp = createMarker(x, y-0.2, z+2.5, "arrow", 0.7, 255, 181, 64, 250) end end end This should work this works, but i need the table thing, because i want that if a player create more than 1 object, then he can interact with just one of them, if he click on the object
Tekken Posted November 28, 2020 Posted November 28, 2020 1 minute ago, Turbesz said: this works, but i need the table thing, because i want that if a player create more than 1 object, then he can interact with just one of them, if he click on the object Well the example I give you let’s you interact with only one object while creating many,doesn’t it?
Turbesz Posted November 28, 2020 Author Posted November 28, 2020 10 minutes ago, Tekken said: Well the example I give you let’s you interact with only one object while creating many,doesn’t it? Yes
Turbesz Posted November 29, 2020 Author Posted November 29, 2020 13 hours ago, Tekken said: Well the example I give you let’s you interact with only one object while creating many,doesn’t it? Yes, but i cannot interact the old one, just with the newly created object
Tekken Posted November 29, 2020 Posted November 29, 2020 Using the example I gave you? you sure? This one: function lerakja(thePlayer) local x, y, z = getElementPosition(thePlayer); local targy = createObject(2167, x+1, y+1, z-1); setElementData(targy, "owner", thePlayer); addEventHandler("onElementClicked", targy, clickfunc); end addCommandHandler("obj", lerakja); function clickfunc(button, state, player) if button == "left" and state == "down" then if getElementType (source ) == "object" and getElementData(source, "owner") == player then local x, y, z = getElementPosition(source); local model = getElementModel(source); outputChatBox("you are use now this object: ID: "..model); local cp = createMarker(x, y-0.2, z+2.5, "arrow", 0.7, 255, 181, 64, 250); end end end
Turbesz Posted November 29, 2020 Author Posted November 29, 2020 14 minutes ago, Tekken said: Using the example I gave you? you sure? This one: function lerakja(thePlayer) local x, y, z = getElementPosition(thePlayer); local targy = createObject(2167, x+1, y+1, z-1); setElementData(targy, "owner", thePlayer); addEventHandler("onElementClicked", targy, clickfunc); end addCommandHandler("obj", lerakja); function clickfunc(button, state, player) if button == "left" and state == "down" then if getElementType (source ) == "object" and getElementData(source, "owner") == player then local x, y, z = getElementPosition(source); local model = getElementModel(source); outputChatBox("you are use now this object: ID: "..model); local cp = createMarker(x, y-0.2, z+2.5, "arrow", 0.7, 255, 181, 64, 250); end end end Yes, but i tried add the move object function: function clickfunc ( button, state, player ) if button == "left" and state == "down" then if getElementType (source ) == "object" and getElementData(source,"owner") == player then local x, y, z = getElementPosition(source) local model = getElementModel(source) bindKey(player,"arrow_u","down",mozgatas) setElementData(source,"mozgathato",true) end end end function mozgatas(player) if getElementData(targy,"owner") == player and getElementData(targy,"mozgathato") == true then local x,y,z = getElementPosition(targy) setElementPosition(targy,x,y-0.5,z) end end i always can move only the new one with this, no matter if i click the old one
Tekken Posted November 29, 2020 Posted November 29, 2020 use moveObject function to move it. just add it into clickfunc function The "onElementClicked" event should check if source has "mozgathato" elemetData
Turbesz Posted November 29, 2020 Author Posted November 29, 2020 8 minutes ago, Tekken said: use moveObject function to move it. just add it into clickfunc function The "onElementClicked" event should check if source has "mozgathato" elemetData if i add moveObject func in clickfunc then i can't move the object with my bindKey, no?
Tekken Posted November 29, 2020 Posted November 29, 2020 Sorry but don't have any idea of what you trying to archive.
Turbesz Posted November 29, 2020 Author Posted November 29, 2020 2 minutes ago, Tekken said: Sorry but don't have any idea of what you trying to archive. i just want move the object which the player clicked with the bindkey func
Tekken Posted November 29, 2020 Posted November 29, 2020 why with bindKey and ton with onElementClicked ?
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