The Killer Posted December 30, 2013 Share Posted December 30, 2013 hello i have a problem in this code: function moveObjectUpOrDown ( ) local x, y, z = getElementPosition ( getElementData(localPlayer, "crea", object) ) if ( getKeyState ( "pgup" ) ) then setElementPosition ( getElementData(localPlayer, "crea", object), x, y, z + 1 ) elseif ( getKeyState ( "pgdn" ) ) then setElementPosition ( getElementData(localPlayer, "crea", object), x, y, z - 1 ) elseif ( getKeyState ( "arrow_r" ) ) then setElementPosition ( getElementData(localPlayer, "crea", object), x + 1, y, z ) elseif ( getKeyState ( "arrow_l" ) ) then setElementPosition ( getElementData(localPlayer, "crea", object), x - 1, y, z ) elseif ( getKeyState ( "arrow_d" ) ) then setElementPosition ( getElementData(localPlayer, "crea", object), x, y - 1, z ) elseif ( getKeyState ( "arrow_u" ) ) then setElementPosition ( getElementData(localPlayer, "crea", object), x, y + 1, z ) end end addCommandHandler("create", function () triggerServerEvent("create", getLocalPlayer()) addEventHandler ( "onClientRender", root, moveObjectUpOrDown ) end ) server side: addEvent("create") addEventHandler("create", root, function (player) local xx,yy,zz = getElementPosition(player) object = createObject(3095, xx,yy,zz - 1) setElementData(player, "crea", object) end ) that when i wrote the command ' create ' nothing happend i want to create object in server side cause in the client side no one can see it so i want everybody can see it so i did this but not working. little help please !! Link to comment
Drakath Posted December 30, 2013 Share Posted December 30, 2013 Try this. Serverside: addCommandHandler("create", function (player) local xx,yy,zz = getElementPosition(player) object = createObject(3095, xx,yy,zz - 1) setElementData(player, "crea", object) end ) function lol (player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x, y, z + 1 ) end bindKey( "pgup", "down", lol ) function lol2 (player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x, y, z - 1 ) end bindKey( "pgdn", "down", lol2 ) function lol3 (player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x + 1, y, z ) end bindKey( "arrow_r", "down", lol3 ) function lol4 (player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x - 1, y, z ) end bindKey( "arrow_l", "down", lol4 ) function lol5 (player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x, y - 1, z ) end bindKey( "arrow_d", "down", lol5 ) function lol6(player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x, y + 1, z ) end bindKey( "arrow_u", "down", lol6 ) Link to comment
The Killer Posted December 30, 2013 Author Share Posted December 30, 2013 it's work but not moving Link to comment
Drakath Posted December 30, 2013 Share Posted December 30, 2013 addCommandHandler("create", function (player) local xx,yy,zz = getElementPosition(player) object = createObject(3095, xx,yy,zz - 1) setElementData(player, "crea", object) end ) function lol (player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x, y, z + 1 ) end function lol2 (player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x, y, z - 1 ) end function lol3 (player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x + 1, y, z ) end function lol4 (player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x - 1, y, z ) end function lol5 (player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x, y - 1, z ) end function lol6(player) local x, y, z = getElementPosition ( getElementData(player, "crea", object) ) setElementPosition ( getElementData(player, "crea", object), x, y + 1, z ) end function binds() bindKey( "pgup", "down", lol ) bindKey( "pgdn", "down", lol2 ) bindKey( "arrow_r", "down", lol3 ) bindKey( "arrow_l", "down", lol4 ) bindKey( "arrow_d", "down", lol5 ) bindKey( "arrow_u", "down", lol6 ) end addEventHandler ( "onResourceStart", getRootElement(), binds ) Link to comment
Sasu Posted December 30, 2013 Share Posted December 30, 2013 (edited) Client: local object = false function moveObjectUpOrDown ( ) local x, y, z = getElementPosition ( object ) if ( getKeyState ( "pgup" ) ) then setElementPosition ( object, x, y, z + 1 ) elseif ( getKeyState ( "pgdn" ) ) then setElementPosition ( object, x, y, z - 1 ) elseif ( getKeyState ( "arrow_r" ) ) then setElementPosition ( object, x + 1, y, z ) elseif ( getKeyState ( "arrow_l" ) ) then setElementPosition ( object, x - 1, y, z ) elseif ( getKeyState ( "arrow_d" ) ) then setElementPosition ( object, x, y - 1, z ) elseif ( getKeyState ( "arrow_u" ) ) then setElementPosition ( object, x, y + 1, z ) end end addCommandHandler("create", function () triggerServerEvent("create", getLocalPlayer()) end ) addEvent("onObjectCreated", true) addEventHandler("onObjectCreated", root, function(value) object = value addEventHandler ( "onClientRender", root, moveObjectUpOrDown ) end) Server: addEvent("create", true) addEventHandler("create", root, function () local xx,yy,zz = getElementPosition(source) object = createObject(3095, xx,yy,zz - 1) triggerClientEvent(source, "onObjectCreated", source, object) end ) Edited December 30, 2013 by Guest Link to comment
The Killer Posted December 30, 2013 Author Share Posted December 30, 2013 (edited) sorry guys it work now thanks Edited December 30, 2013 by Guest Link to comment
DNL291 Posted December 30, 2013 Share Posted December 30, 2013 Replace addEvent("create") With: addEvent("create", true) Link to comment
Sasu Posted December 30, 2013 Share Posted December 30, 2013 A little mistake, copy the server side again. Link to comment
The Killer Posted December 30, 2013 Author Share Posted December 30, 2013 yes i know, before you post i fix it, and thanks Link to comment
The Killer Posted December 31, 2013 Author Share Posted December 31, 2013 there is another problem, that no one can see the object moving, except me, what is wrong ? Link to comment
Jusonex Posted December 31, 2013 Share Posted December 31, 2013 That's because you're calling setElementPosition clientside. If you want the objects to be synced, you'll have to tell the other clients the current position of the object you're moving (--> triggerServerEvent). But it's not recommendable to do this every frame since that would lead to a huge amount of network traffic. Instead you can sync the position every 2 seconds for example. addEvent("onObjectCreated", true) addEventHandler("onObjectCreated", root, function(value) object = value addEventHandler ( "onClientRender", root, moveObjectUpOrDown ) setTimer( function() local x, y, z = getElementPosition(object) triggerServerEvent("positionSync", object, x, y, z) end, 2000, 0 ) end) Server: addEvent("positionSync", true) addEventHandler("positionSync", root, function(x, y, z) setElementPosition(source, x, y, z) end ) Attention: Due to security issues, you should check whether the player is allowed to set the position. Otherwise this event could be abused to teleport all elements. Link to comment
TAPL Posted December 31, 2013 Share Posted December 31, 2013 Additional to what Jusonex said, you shouldn't trigger unless the position changed. Link to comment
The Killer Posted December 31, 2013 Author Share Posted December 31, 2013 Thanks Jusonex. Link to comment
DNL291 Posted December 31, 2013 Share Posted December 31, 2013 You can also use setElementData instead of triggerServerEvent, and then use onElementDataChange at the serverside. 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