Hi Tekken,
Whenever you attach two elements you can provide offsets relative to the element you are attaching to. Because of this you it shouldn't be too difficult what you're trying to achieve.
I've created a simple script to demonstrate this below. You can provide an offset x, y and z. To spawn something in front of the player, you would have to use y.
attachElements(object, player,0,3,0) -- Y is 3, you could change this to be smaller (closer to the player) or bigger (farther away)
Here's a quick demo script. You can use /cao <objectid> to create something and then /do to detach it in case something goes wrong.
-- Creates object and attaches to player
function createAttachedObjectInFrontOfPlayer(player,objectid)
local x,y,z = getElementPosition(player)
local object = createObject(objectid, x,y,z+10)
return attachElements(object, player,0,3,0)
end
-- Command to create attached object
addCommandHandler('cao',
function(player, cmd, objectid)
if not objectid then
outputChatBox("You didn't provide an object ID!",player,255,0,0)
return false
else
createAttachedObjectInFrontOfPlayer(player,tonumber(objectid))
end
end
)
-- Detaches whatever you attached to the player.
addCommandHandler('do',
function(player)
local attached_elements = getAttachedElements(player)
for i, element in ipairs(attached_elements) do
detachElements(element,player)
end
end
)