Jump to content

createObject server-sided


Recommended Posts

Hi,

I'm not a new scripter and know a bit lua but I've never made any server-sided script though. I'd like to know how to make a simple (I guess) script. For example when I type "/create" in chatbox the script checks which element is selected (in my editor in this case) and it creates an object with the same position and rotation what the selected element (object/vehicle).

Thanks for any help ;)

regards,

2kristof2

Link to comment

  
objects = {} 
addCommandHandler("create",function (player,cmd,oid) 
      if oid then 
      local x,y,z = getElementPosition(player) 
      local rx,ry,rz = getElementRotation(player) 
      objects[player] = createObject(oid,x,y,z,rx,ry,rz) 
           if isElement(objects[player]) then 
           destroyElement(objects[player]) 
           end 
      end 
end 
) 
Link to comment

Well, you did it, didn't you? iMr.Dawix~# provided you the code, which should work.

objects = {} 
addCommandHandler("create", 
    function (player,cmd,oid) 
        if tonumber(oid) then 
            if objects[player] then 
                destroyElement(objects[player]) 
            end 
            local x,y,z = getElementPosition(player) 
            local rx,ry,rz = getElementRotation(player) 
            objects[player] = createObject(tonumber(oid),x,y,z,rx,ry,rz) 
        end 
    end 
) 

/create [iD]

Link to comment

I'm gonna give you functions which you need to use to make that. Try to make it and if you have errors, post here.

exports.editor_main:getSelectedElement() -- To get element you selected in editor. 
getElementPosition -- Use it on exported function to get position of an element you selected. 
getElementRotation -- Use it on exported function to get rotation of an element you selected. 
addCommandHandler 
createObject 
exports.editor:import() -- To import back to editor if you wish. 

Link to comment

I have no idea how to make it...

Just made script which has none sense:

  
 objects = {} 
addCommandHandler("cre",function (cmd, oid) 
local selectedElement = exports.editor_main:getSelectedElement() 
      if oid then 
      local x,y,z = exports.edf:edfGetElementPosition(selectedElement) 
      local rx, ry, rz = exports.edf:edfGetElementRotation(selectedElement) 
      objects[selectedElement] = createObject(oid,x,y,z,rx,ry,rz) 
           if isElement(objects[selectedElement]) then 
           destroyElement(objects[selectedElement]) 
           end 
      end 
end 
) 
  

when I type "/cre ID" I noticed the following warnings:

qH6wi0X.jpg

Link to comment

I didn't give you EDF functions? What are you doing lol?

objects = {} 
addCommandHandler("cre",function (cmd, oid) 
      local selectedElement = exports.editor_main:getSelectedElement() 
      if oid then 
      local x,y,z = getElementPosition(selectedElement) 
      local rx, ry, rz = getElementRotation(selectedElement) 
      objects[selectedElement] = createObject(oid,x,y,z,rx,ry,rz) 
           if isElement(objects[selectedElement]) then 
           destroyElement(objects[selectedElement]) 
           end 
      end 
end 
) 

Link to comment

I've been fiddling with this case, I've tested out some methods and came to a solution:

Client:

addCommandHandler("cre", 
    function(cmd, oid) 
        local selectedElement = exports.editor_main:getSelectedElement() 
        if getElementType(selectedElement) == "object" and tonumber(oid) then 
            local x, y, z = getElementPosition(selectedElement) 
            local rx, ry, rz = getElementRotation(selectedElement) 
            triggerServerEvent("e:import", root, tonumber(oid), x, y, z, rx, ry, rz) 
        end 
    end 
) 

Server:

addEvent("e:import", true) 
addEventHandler("e:import", root, 
    function(id, x, y, z, rx, ry, rz) 
        local element = createObject(id, x, y, z, rx, ry, rz) 
        exports.editor:import(element) 
        destroyElement(element) 
    end 
) 

Link to comment

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.
×
×
  • Create New...