If an entity is attached to another, NetworkRequestControlOfEntity() and NetworkRequestControlOfNetworkId() fail unless the entity was created by the player.
AttachEntityToEntity(entityOne, entityTwo, …)
In the example above a Player can take control of entityTwo but not entityOne whilst they are attached.
Once the entities are detached, a Player can take control of entityOne . However, if they were to reattach the entity themselves, they would lose control again and be unable to request it back until it has been detached by the player who created it. They would still have control of entityTwo though.
Anyone know if this is a bug or I am not doing something correctly?
Server: Latest artifact; Standard, Legacy and Infinity
EDIT: I realise I may have worded this badly, here’s a breakdown.
Player 1 creates two entities and attaches them.
Player 2 attempts to take control of both entities to detach them.
They will only get control of the entity that was attached to.
Player 1 detaches the entities.
Player 2 can now take control of both entities without any issue.
Player 2 attaches the entities back together whilst they have control.
Player 2 can yet again only get control of the entity that was attached to
Getting a reference to the entity isn’t the issue, I have the entity. It’s that as soon as it’s attached to another entity only its creator can control it; for example detach it or delete it.
To piggyback this topic:
I am not so sure that this issue is connected to FiveM but rather a standard GTA V issue.
You can test this using a towtruck (either tow or tow2, not the flatbed). When player 2 attaches an entity that is owned by player 1 there is a chance that the attached vehicle ports back to original position upon release.
This issue is about a year old and happened on the standard version. It is theoretically possible that this has changed but I wouldn’t count on it.
If you are lucky a game event might fire if you attach an entity (or if you attach the entity yourself using code you’ll be just fine).
Now I think this might be a OneSync-only-native: NETWORK_GET_ENTITY_OWNER - Cfx.re Docs
The following works only with OneSync. You can use this for whichever attaching-function you wanna use. It should work (didn’t test it though).
Client-side player 2
AddEventHandler('TriggerAttaching', function(entity1, entity2) -- as named in the Attach-native
local ent1net = PedToNet(ent1net)
local ent2net = PedToNet(ent2net)
TriggerServerEvent('AttachingServerside', ent1net, ent2net)
AddEventHandler('LocalAttachEntityToEntity', function(ent1net, ent2net)
local entity1 = NetToPed(ent1net)
local entity2 = NetToPed(ent2net)
-- do whatever you want here
AddEventHandler('AttachingServerside', function(ent1net, ent2net)
local entity1 = NetworkGetEntityFromNetworkId(ent1net)
local entity2 = NetworkGetEntityFromNetworkId(ent2net)
local owner1 = NetworkGetEntityOwner(entity1)
local owner2 = NetworkGetEntityOwner(entity2)
TriggerClientEvent('LocalAttachEntityToEntity', owner1, ent1net, ent2net)
TriggerClientEvent('LocalAttachEntityToEntity', owner2, ent1net, ent2net)
Update: OK I’ve worked this out.
When someone is in a vehicle anything attached to that vehicle another player cannot control as control will always revert back to the player in the vehicle.
When something is attached to something, any player that didn’t attach it will lose it’s entity id. So you can use netid instead to ensure every player can still reference this entity.