Jump to content

How to attach players to vehicles...


The_Stig

Recommended Posts

So... I got it working =) It is really simple.

!!!!I WOULD RECOMMEND WAITING FOR MY FINAL SCRIPT RELEASE!!!!

I need to make it all ONE command, with A LOT more options.

This sticks your player to a vehicle, with XYZ relative to the vehicle they are in.

  
function cmd_stick ( player, commandName, player2nick, x, y, z ) 
    local player2 = getPlayerFromNick ( player2nick ) 
    local vehicle = getPlayerOccupiedVehicle  ( player2 ) 
    attachElementToElement ( player, vehicle, x, y, z  ) 
end 
addCommandHandler ( "stick", cmd_stick ) 
  
function cmd_unstick ( player, commandName ) 
    local vehicle = getElementAttachedTo ( player ) 
    detachElementFromElement ( vehicle ) 
end 
addCommandHandler ( "unstick", cmd_unstick ) 
  

Link to comment
So... I got it working =) It is really simple.

!!!!I WOULD RECOMMEND WAITING FOR MY FINAL SCRIPT RELEASE!!!!

I need to make it all ONE command, with A LOT more options.

This sticks your player to a vehicle, with XYZ relative to the vehicle they are in.

  
function cmd_stick ( player, commandName, player2nick, x, y, z ) 
    local player2 = getPlayerFromNick ( player2nick ) 
    local vehicle = getPlayerOccupiedVehicle  ( player2 ) 
    attachElementToElement ( player, vehicle, x, y, z  ) 
end 
addCommandHandler ( "stick", cmd_stick ) 
  
function cmd_unstick ( player, commandName ) 
    local vehicle = getElementAttachedTo ( player ) 
    detachElementFromElement ( vehicle ) 
end 
addCommandHandler ( "unstick", cmd_unstick ) 
  

Doesn't work.

Debugged it and get errors on line 4 and 10.

Link to comment

Works in our script...not sure, I'm working on it right now, to get it to store if the player is stuck or not, and then it will check when you try to /stick again so it can unstick you then restick automatically, or just unstick all together if no parameters specified.

If it errors on line 4 and 10 then its not sending the player variable correctly when the player does the command.

Are you doing /stick name 0 0 0 ?

Heres the updated code that is running/working on our server [Free Roam Chaos]:

function cmd_stick ( player, commandName, player2nick, x, y, z ) 
    local player2 = getPlayerFromNick ( player2nick ) 
    local vehicle = getPlayerOccupiedVehicle  ( player2 ) 
    if isPlayerInVehicle ( player ) then 
        removePlayerFromVehicle ( player ) 
        detachElementFromElement( player ) 
        attachElementToElement ( player, vehicle, x, y, z  ) 
    else 
        detachElementFromElement( player ) 
        attachElementToElement ( player, vehicle, x, y, z  ) 
    end 
end 
addCommandHandler ( "stick", cmd_stick ) 
  
function cmd_stickv ( player, commandName, player2nick, x, y, z ) 
    if isPlayerInVehicle ( player ) then 
        local playerv = getPlayerOccupiedVehicle  ( player ) 
        local player2 = getPlayerFromNick ( player2nick ) 
        local vehicle = getPlayerOccupiedVehicle  ( player2 ) 
        detachElementFromElement( playerv ) 
        attachElementToElement ( playerv, vehicle, x, y, z ) 
    else 
        detachElementFromElement( player ) 
        attachElementToElement ( player, vehicle, x, y, z  ) 
    end 
end 
addCommandHandler ( "stickv", cmd_stickv ) 
  
function cmd_unstick ( player ) 
    element = getElementAttachedTo ( player ) 
    detachElementFromElement( player ) 
    if isPlayerInVehicle ( player2 ) then 
        local vehicle = getPlayerOccupiedVehicle  ( player ) 
        detachElementFromElement( vehicle ) 
    else 
        detachElementFromElement( player ) 
    end 
end 
addCommandHandler ( "unstick", cmd_unstick ) 

/stick Playername X Y Z - sticks you to the players vehicle only (removes you from vehicle if your in one)

/stickv Playername X Y Z - sticks you and your vehicle to the players vehicle

/unstick - unsticks your car/player

X Y Z is the offset from the other players car, usually start out with a Z of 1 and you should be good. Play with the X (left/right) Y(front/back) Z (above/below) to get exactly where you want to be on the car.

Hope this works for someone.

Link to comment

Yeah that would take a complex math calculation, Me and LeetWoovie are trying to work on it now but it has to calculate the cars position and the players position and the cars rotation then has to find exactly where your standing and since GTA uses relative N S E W not relative to the car its kind of hard to do it without some complex math calculation...of some sort, that I'm not exactly sure on. I will see what happens with our script.

Link to comment

there is a way to have it just detect when you jump onto a car or a vehicle..

I don't use it, since it is kinda buggy, and if you want to ride on a plane, and move once you just fall through, and car sticking is buggy..

I got the clientside code from AlienX's server, and I have made my own server side handeler for it.

Clientside Code (CREDITS GO TO THE ALIENX SERVER):

  
local contactVehicle    = false 
local localGlueState    = false 
local controlDelay      = 5  --frames 
local jumpDelay         = 20 --frames 
local movementControls  = {"forwards","backwards","left","right"} 
  
local localPlayer       = getLocalPlayer() 
local localRootElement  = getRootElement() 
local glueFreeFrames    = 0 
  
function checkVehicleGlue() 
    if glueFreeFrames > 0 then 
        glueFreeFrames = glueFreeFrames - 1 
        return 
    end 
     
    contactVehicle = getPlayerContactElement(localPlayer) 
    if isLocalPlayerMoving() then 
        setGlueState(false) 
    else 
        if localGlueState == true then return end 
        if contactVehicle then 
            local pX, pY, pZ = getElementPosition(localPlayer) 
            local vX, vY, vZ = getElementPosition(contactVehicle) 
            local vrX, vrY, vrZ = getVehicleRotation(contactVehicle) 
            local vrZ = math.rad(vrZ) 
  
            local offsetX, offsetY, offsetZ = (pX-vX)*math.sin(vrZ), (pY-vY)*math.cos(vrZ), pZ-vZ 
             
            setGlueState(true, contactVehicle, offsetX, offsetY, offsetZ) 
        end 
    end 
end 
  
addEventHandler("onClientRender", localRootElement, checkVehicleGlue) 
  
function isLocalPlayerMoving() 
    --if any directional keys are down, the player is moving 
    for i,control in ipairs(movementControls) do 
        if getControlState(control) == true then 
            glueFreeFrames = controlDelay 
            return true 
        end 
    end 
    --if jump was pressed, we will disable glue checking for a small time 
    if getControlState("jump") then 
        glueFreeFrames = jumpDelay 
        return true 
    end 
    --if the player has a jetpack and the sprint key is down, he's ascending 
    if doesPlayerHaveJetPack(localPlayer) and getControlState("sprint") then 
        return true 
    end 
     
    return false 
end 
  
function setGlueState(newGlueState, toElement, offsetX, offsetY, offsetZ) 
    if newGlueState ~= localGlueState then 
        localGlueState = newGlueState 
         
        if newGlueState == true then 
triggerServerEvent ( "onClientGlue", getLocalPlayer(), toElement, offsetX, offsetY, offsetZ )  
             
        else 
            triggerServerEvent("onClientUnglue", getLocalPlayer(), toElement ) 
        end      
    end 
end 
  
  

Servside Code: (Credit goes to me)

function glueonHandler ( toElement, offsetX, offsetY, offsetZ ) 
local sourcename = getClientName ( source ) 
attachElementToElement ( source, toElement, offsetX, offsetY, offsetZ ) 
end 
addEvent("onClientGlue", true) 
addEventHandler("onClientGlue", getRootElement(), glueonHandler) 
  
  
function glueoffHandler ( toElement ) 
local sourcename = getClientName ( source ) 
detachElementFromElement ( source, toElement ) 
end 
addEvent("onClientUnglue", true) 
addEventHandler("onClientUnglue", getRootElement(), glueoffHandler) 

Haven't really tested it.. since all I did was do this just to post here, and don't actually use it.. But it seems to work decently. Not that well though, since moving once will detach you from a plane..

Link to comment

I had a go at this kind of thing - it produces some weird results (

).

The fences are part of the map (manager). The barrels I create. Neither has collision while attached, but the created vehicles do (dinghies trapped in the "cage" and then stuck to my car).

Edit : I have to go to the remote cam because the attached dinghies block the driver view. Also, the weight of the dingies on the car pushes the front down enough for it to lift the rear end.

Link to comment
there is a way to have it just detect when you jump onto a car or a vehicle..

I don't use it, since it is kinda buggy, and if you want to ride on a plane, and move once you just fall through, and car sticking is buggy..

I got the clientside code from AlienX's server, and I have made my own server side handeler for it.

Clientside Code (CREDITS GO TO THE ALIENX SERVER):

  
local contactVehicle    = false 
local localGlueState    = false 
local controlDelay      = 5  --frames 
local jumpDelay         = 20 --frames 
local movementControls  = {"forwards","backwards","left","right"} 
  
local localPlayer       = getLocalPlayer() 
local localRootElement  = getRootElement() 
local glueFreeFrames    = 0 
  
function checkVehicleGlue() 
    if glueFreeFrames > 0 then 
        glueFreeFrames = glueFreeFrames - 1 
        return 
    end 
     
    contactVehicle = getPlayerContactElement(localPlayer) 
    if isLocalPlayerMoving() then 
        setGlueState(false) 
    else 
        if localGlueState == true then return end 
        if contactVehicle then 
            local pX, pY, pZ = getElementPosition(localPlayer) 
            local vX, vY, vZ = getElementPosition(contactVehicle) 
            local vrX, vrY, vrZ = getVehicleRotation(contactVehicle) 
            local vrZ = math.rad(vrZ) 
  
            local offsetX, offsetY, offsetZ = (pX-vX)*math.sin(vrZ), (pY-vY)*math.cos(vrZ), pZ-vZ 
             
            setGlueState(true, contactVehicle, offsetX, offsetY, offsetZ) 
        end 
    end 
end 
  
addEventHandler("onClientRender", localRootElement, checkVehicleGlue) 
  
function isLocalPlayerMoving() 
    --if any directional keys are down, the player is moving 
    for i,control in ipairs(movementControls) do 
        if getControlState(control) == true then 
            glueFreeFrames = controlDelay 
            return true 
        end 
    end 
    --if jump was pressed, we will disable glue checking for a small time 
    if getControlState("jump") then 
        glueFreeFrames = jumpDelay 
        return true 
    end 
    --if the player has a jetpack and the sprint key is down, he's ascending 
    if doesPlayerHaveJetPack(localPlayer) and getControlState("sprint") then 
        return true 
    end 
     
    return false 
end 
  
function setGlueState(newGlueState, toElement, offsetX, offsetY, offsetZ) 
    if newGlueState ~= localGlueState then 
        localGlueState = newGlueState 
         
        if newGlueState == true then 
triggerServerEvent ( "onClientGlue", getLocalPlayer(), toElement, offsetX, offsetY, offsetZ )  
             
        else 
            triggerServerEvent("onClientUnglue", getLocalPlayer(), toElement ) 
        end      
    end 
end 
  
  

Servside Code: (Credit goes to me)

function glueonHandler ( toElement, offsetX, offsetY, offsetZ ) 
local sourcename = getClientName ( source ) 
attachElementToElement ( source, toElement, offsetX, offsetY, offsetZ ) 
end 
addEvent("onClientGlue", true) 
addEventHandler("onClientGlue", getRootElement(), glueonHandler) 
  
  
function glueoffHandler ( toElement ) 
local sourcename = getClientName ( source ) 
detachElementFromElement ( source, toElement ) 
end 
addEvent("onClientUnglue", true) 
addEventHandler("onClientUnglue", getRootElement(), glueoffHandler) 

Haven't really tested it.. since all I did was do this just to post here, and don't actually use it.. But it seems to work decently. Not that well though, since moving once will detach you from a plane..

Just tried this and it works great.

We mainly use it for a bit of fun really using planes and this is perfect.

Thanks :D :D

Link to comment

It still seemed wrong even in that case when I used it.

Like say you jumped on the front of the car it'd glue you a metre off the left of it or something similar. I fixed it by changing the math used but it's strange no-one else has mentioned it.

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...