Sync rope between players

hello everyone, i was making a script in which i used addrope and attachentitestorope natives. The thing everything is working fine on my point of view, but when someone is next to me, they cannot see the rope . How can i make so that the rope is visible for everyone ?

2 Likes

What you most likely have to do is sync the calls to AddRope and AttachEntitiesToRope across clients. So on your client code instead of doing this:

AddRope(x,y,z,a,b,c.......)
AttachEntitiesToRope(a,b,c,d,e, .....)

You’ll want to do this

-- client
-- call this where you were attaching the rope b4
TriggerServerEvent("useServerToAttachEntitiesToRope", entity1, entity2, x,y,z,a,b,c,1,2,3...)

-- this will get called from the server across every client
RegisterNetEvent('clientAttachEntitiesToRope')
AddEventHandler('clientAttachEntitiesToRope', function(entity1, entity2, x,y,z,a,b,c,1,2,3...)
  AddRope(x,y,z,a,b,c.......)
  AttachEntitiesToRope(a,b,c,d,e, .....)
end)


-- server
RegisterServerEvent('useServerToAttachEntitiesToRope')
AddEventHandler('useServerToAttachEntitiesToRope', function(entity1, entity2, x,y,z,a,b,c,1,2,3...)
    players = GetPlayers()

    -- we do this for all players, because im assuming you would want the rope to appear attached for everyone
    for k,v in pairs(players) do
        TriggerClientEvent("clientAttachEntitiesToRope", v, entity1, entity2, x,y,z,a,b,c,1,2,3...)
    end
end)

thanks for your reply.
Thats what i tought, but entities are different for everyone isn’t it ?

And i don’t know if you use that code somewhere on your server but instead of loopin just do a -1 ti trigger it for everyone

RegisterServerEvent('useServerToAttachEntitiesToRope')
AddEventHandler('useServerToAttachEntitiesToRope', function(entity1, entity2, x,y,z,a,b,c,1,2,3...)
    -- we do this for all players, because im assuming you would want the rope to appear attached for everyone
        TriggerClientEvent("clientAttachEntitiesToRope", -1, entity1, entity2, x,y,z,a,b,c,1,2,3...)
end)

Oh good catch, I actually am using that for something else. I’ll swap that out :grin:

To be honest, I’m not entirely sure how entities are shared across clients. I know when you create a ped/object you have the option to set it as networked. What does this actually do under the hood? Can we replicate that for your rope? Does it just simultaneously create multiple instances of the entity across clients or is it actually somehow able to use one singular entity across clients? If it’s using the prior (my solution should work), then even though there are multiple instances of the entity across clients, you can still simulate it to seem like it’s one object by going through the server when ever you want to interact with it. If it’s the latter, then you would need to find out how to set an entity as networked (probably NetworkRegisterEntityAsNetworked but I couldn’t get it to work using latest binaries) then I’d imagine anything you do on one client with automatically be synced across the network.

Did you end up testing out what I wrote?

You can pass Entities to network through Network natives.
NetworkGetNetworkIdFromEntity(entity) to get the id.
NetworkGetEntityFromNetworkId(id) to get the entity back.