[HELP] How to sync objects between players

Hello guys,

I need a help to sync objects between multiple players connected on my server, let me explain you my situation:

I have an event called by server-side that create objects for every clients connected, for example:


RegisterNetEvent("gmz:objects:onCreate")
AddEventHandler("gmz:objects:onCreate", function(obj)

    RequestModel(obj.Model)

    while not HasModelLoaded(obj.Model) do
        Wait(1)
    end

    local objectId = GetClosestObjectOfType(obj.X, obj.Y, obj.Z, 2.5, GetHashKey(obj.Model), false, false, true)
    local object = nil
    if objectId > 0 then
        print('Object '..obj.Model..' already exists, ID: '..objectId)
    else
        object = ObjToNet(CreateObjectNoOffset(obj.Model, obj.X, obj.Y, obj.Z, true, true, false))
        SetEntityRotation(object, obj.Pitch, obj.Roll, obj.Yaw, 0, true)
        print('Object'..obj.Model..' does not exists, creating new one, ID: '..object)
        
    end
    
end)

and the server side call:

 TriggerClientEvent("gmz:objects:onCreate", -1, obj)

If I just have one player connected, it is ok, just create one object, but if I have more then one player connected, it creates the objects according to amount of connected players, like below;

How can I just create one object for connected players?

Thank you!

1 Like

i know it’s late but it doing this because you create an object on client and next you say to the server to re-create an object on all clients (including the player that send the event) so you have multiple events

You can do as @NessXo874 said, or you can just create the object directly on the server-side…