Jump to content

Cargobob Carry Script HELP


Recommended Posts

Hello!

I have made a client-side script that allows players in a Cargobob to carry other players in vehicles and also release them by using attach/detach elements. However, it is not fully working. My script is below:

  
  
attached = false -- Variable to Check Before Attaching/Detaching 
carrying = false -- Player Being Carried 
vehicle = 548 -- Carrying Vehicle (Cargobob) 
outputChatBox("values set "..getPlayerName(getLocalPlayer()).."") 
  
-- Vehicles NOT Able to Be Carried 
local unAbleList =  
{ 
    {592},{577},{511},{548},{512},{593},{425},{520},{417},{487},{553},{488},{497},{563},{476},{447},{519},{460},{469},{513} -- All the Aircraft 
} 
  
function connect() 
    local player = getLocalPlayer() 
    local x,y,z = getElementPosition(player) 
    outputChatBox("trying to connect...") 
    if (attached == false) then -- If NOT Carrying Vehicle... 
        if (getPedOccupiedVehicle(player) == vehicle) then -- If Player is in Carrier Vehicle... 
            outputChatBox("in heli...") 
            local target = false 
            local tx = 0 
            local ty = 0 
            local tz = 0 -- Not Used 
            local breaker = 0 
            local playList = getElementsByType("player") 
            local rnd = 0 
            repeat 
                breaker = breaker + 1 
                rnd = math.random(1, #playList) 
                target = playList[rnd] 
                tx,ty,tz = getElementPosition(target) 
                outputChatBox("breaker: "..breaker.."") 
            until ( ((getDistanceBetweenPoints2D(x,y, tx,ty) < 20) and (getPedOccupiedVehicle(target)) and ( (target == player) == false)) or (breaker > 100) ) -- Search Through Players, Find One Within 3 Pixels, Check Vehicle... 
            attachElements(target, player, 0, 0, -2) 
            carrying = target 
            attached = true 
            outputChatBox("attached! to: "..getPlayerName(target).."") 
        end 
    elseif (attached == true) then -- Else if Carrying Vehicle... 
        outputChatBox("detached!") 
        detachElements(player) 
        attached = false 
        carrying = false 
    end  
end 
  
function bindFunc() 
    bindKey("2", "down", connect) 
end 
addCommandHandler("attacher", bindFunc) 
  

It doesn't work, but I can't find why. Everything looks laid out properly and everything but it's not working. Can someone please help me with this script? Any help is greatly appreciated.

EDIT:

I kindly ask that you do not steal this script (in-progress) or anything, I just want help with it. :)

UNSOLVED!

Link to comment

1st, vehicle is defined as a number:

         if (getElementModel(getPedOccupiedVehicle(player)) == vehicle) then -- If Player is in Carrier Vehicle... 

2nd, to search te player I think it would be better using a for loop instead of a repeat:

            outputChatBox("in heli...") 
            local target 
            for i, tplayer in ipairs(getElementsByType('player')) do 
                local tx, ty, tz = getElementPosition(tplayer) 
                -- Search Through Players, Find One Within 3 Pixels, Check Vehicle... 
                if (tplayer ~= player) and getPedOccupiedVehicle(tplayer) and getDistanceBetweenPoints2D(x,y,tx,ty) < 20 then 
                    target = tplayer 
                    break 
                end 
            end 
            attachElements(target, player, 0, 0, -2) 
            carrying = target 

But why clientside? Shouldn't it be serverside?

Link to comment

Okay, now all the script seems to work correctly and the "detach" etc message comes up, but the vehicles do not appear to be attached in-game.

client.lua

  
  
function starty() 
attached = false -- Variable to Check Before Attaching/Detaching 
carrying = false -- Player Being Carried 
vehicle = 548 -- Carrying Vehicle (Cargobob) 
outputChatBox("values set "..getPlayerName(getLocalPlayer()).."") 
end 
addEventHandler("onClientResourceStart", getRootElement(), starty) 
  
-- Vehicles NOT Able to Be Carried 
local unAbleList =  
{ 
    {592},{577},{511},{548},{512},{593},{425},{520},{417},{487},{553},{488},{497},{563},{476},{447},{519},{460},{469},{513} -- All the Aircraft 
} 
  
function connect() 
    local player = getLocalPlayer() 
    local x,y,z = getElementPosition(player) 
    outputChatBox("trying to connect...") 
    if (attached == false) then -- If NOT Carrying Vehicle... 
        if (getElementModel(getPedOccupiedVehicle(player)) == vehicle) then -- If Player is in Carrier Vehicle... 
            outputChatBox("in heli...") 
            local target = false 
            for i, tplayer in ipairs(getElementsByType('player')) do 
                local tx, ty, tz = getElementPosition(tplayer) 
                -- Search Through Players, Find One Within 3 Pixels, Check Vehicle... 
                if (tplayer ~= player) and getPedOccupiedVehicle(tplayer) and getDistanceBetweenPoints2D(x,y,tx,ty) < 5 then 
                    target = tplayer 
                    break 
                end 
            end 
            carrying = target -- Search Through Players, Find One Within 3 Pixels, Check Vehicle... 
            if (target) then 
                triggerServerEvent("onVehicleAttach", getRootElement(), target, player) 
                --attachElements(target, player, 0, 0, -2) 
                carrying = target 
                attached = true 
                outputChatBox("attached! to: "..getPlayerName(target).."") 
            end 
        end 
    elseif (attached == true) then -- Else if Carrying Vehicle... 
        outputChatBox("detached!") 
        triggerServerEvent("onVehicleDetach", getRootElement(), carrying) 
        --detachElements(player) 
        attached = false 
        carrying = false 
    end  
end 
  
function bindFunc() 
    bindKey("2", "down", connect) 
end 
addCommandHandler("attacher", bindFunc) 
  
function bindUs(source, commandname, target) 
    --attachElements(target, getLocalPlayer(), 0,0,-2) 
    attachElements(getPedOccupiedVehicle(target), getPedOccupiedVehicle(getLocalPlayer()), 0, 0, -2) 
end 
addCommandHandler("put", bindUs) 
  

server.lua

  
addEvent("onVehicleAttach", true) 
addEvent("onVehicleDetach", true) 
  
function attach(target, player) 
    attachElements(target, player, 0, 0, -2) 
    outputChatBox("ATTACHED "..getPlayerName(target).." to "..getPlayerName(player).."") 
end 
addEventHandler("onVehicleAttach", getRootElement(), attach) 
  
function detach(target) 
    detachElements(target) 
    outputChatBox("DEEEETACHED") 
end 
addEventHandler("onVehicleDetach", getRootElement(), detach) 
  

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