Jump to content

[HELP]Trunk Script


ViRuZGamiing

Recommended Posts

Posted

Hello,

Here's my script;

Client-sided

local screenWidth, screenHeight = guiGetScreenSize() 
local root = getRootElement(); 
local player = getLocalPlayer(); 
local minDistance = 5; 
local interactButton = "M" 
local isDebug = "false"; 
local lookAtVehicle = nil 
    
        
function interactVehicle() 
    lookAtVehicle = getPedTarget(player); 
     
    if not (isPedInVehicle(player)) then 
        if (lookAtVehicle) and (getElementType(lookAtVehicle) == "vehicle" ) then 
           
            local vx, vy, vz = getElementPosition(lookAtVehicle); 
            local rxv, ryv, rzv = getElementRotation(lookAtVehicle) 
            local px, py, pz = getElementPosition(player); 
            local distanceToVehicle = getDistanceBetweenPoints3D(px, py, pz, vx, vy, vz); 
                 
            if (isDebug == "true") then 
                dxDrawText("Vehicle: " .. getVehicleName(lookAtVehicle), screenWidth/2, screenHeight/2 - 110, screenWidth/2, screenHeight/2 - 110, tocolor(255, 255, 255, 255), 1, "arial", "center", "center", false, false, true); 
                if(isInteractableVehicle(lookAtVehicle) == "true") then 
                    dxDrawText("Interaction possible for this vehicle.", screenWidth/2, screenHeight/2 - 90, screenWidth/2, screenHeight/2 - 90, tocolor(128, 0, 128, 255), 1, "arial", "center", "center", false, false, true); 
                else 
                    dxDrawText("No interaction possible for this vehicle!", screenWidth/2, screenHeight/2 - 90, screenWidth/2, screenHeight/2 - 90, tocolor(128, 0, 0, 255), 1, "arial", "center", "center", false, false, true); 
                end 
                dxDrawLine3D(vx, vy, vz, px, py, vz, tocolor(0, 120, 0, 150), 2); 
            end 
                 
            if(isInteractableVehicle(lookAtVehicle) == "true") then                 
                if (distanceToVehicle < minDistance) and (getVehicleRelatedPosition()) then 
                    if not(isVehicleLocked(lookAtVehicle)) then 
                        local checkDoor = getVehicleDoorState(lookAtVehicle, getVehicleRelatedPosition()); 
                         
                        if (checkDoor ~= 4 ) then 
                            local doorRatio = getVehicleDoorOpenRatio(lookAtVehicle, getVehicleRelatedPosition()); 
                             
                            if (doorRatio <= 0) then 
                                dxDrawText("#FFFFFFPress #00FF00" .. interactButton .. "#FFFFFF to open #00FF00" .. getDoorName() .. "#FFFFFF!", screenWidth/2, screenHeight/2, screenWidth/2, screenHeight/2, tocolor(255, 255, 255, 255), 1, "arial", "center", "center", false, false, false, true, true); 
                            else 
                                dxDrawText("#FFFFFFPress #00FF00" .. interactButton .. "#FFFFFF to close #00FF00" .. getDoorName() .. "#FFFFFF!", screenWidth/2, screenHeight/2, screenWidth/2, screenHeight/2, tocolor(255, 255, 255, 255), 1, "arial", "center", "center", false, false, false, true, true); 
                            end  
                        end 
                    else 
                        dxDrawText("#FF0000 Vehicle is locked!", screenWidth/2, screenHeight/2, screenWidth/2, screenHeight/2, tocolor(255, 255, 255, 255), 1, "arial", "center", "center", false, false, false, true, true); 
                    end            
                end 
            end 
        end 
    end 
end 
addEventHandler("onClientRender", root, interactVehicle); 
  
  
function getVehicleRelatedPosition()     
    if (lookAtVehicle) and (getElementType(lookAtVehicle) == "vehicle") then    
        local vx, vy, vz = getElementPosition(lookAtVehicle); 
        local rxv, ryv, rzv = getElementRotation(lookAtVehicle); 
        local px, py, pz = getElementPosition(player); 
        local anglePlayerToVehicle = math.atan2(px - vx, py - vy); 
        local formattedAnglePlayerToVehicle = math.deg(anglePlayerToVehicle) + 180; 
        local vehicleRelatedPosition = formattedAnglePlayerToVehicle + rzv; 
         
        if (vehicleRelatedPosition < 0) then 
            vehicleRelatedPosition = vehicleRelatedPosition + 360 
        elseif (vehicleRelatedPosition > 360) then 
            vehicleRelatedPosition = vehicleRelatedPosition - 360 
        end 
         
        if (vehicleRelatedPosition >= 325) and (vehicleRelatedPosition <= 360)  or (vehicleRelatedPosition >= 0) and (vehicleRelatedPosition <= 31) then 
            return 1; 
        end 
    end 
     
    return nil; 
end 
  
  
function getDoorName() 
    elseif (getVehicleRelatedPosition() == 1) then 
        return "trunk"; 
end 
     
    return ""; 
end 
  
function isInteractableVehicle(lookAtVehicle) 
    local vehicle = lookAtVehicle; 
    local interactableVehicles = {  602, 545, 496, 517, 401, 410, 518, 600, 527, 436, 589, 580, 
                                    419, 439, 533, 549, 526, 491, 474, 445, 467, 604, 426, 507, 
                                    547, 585, 405, 587, 409, 466, 550, 492, 566, 546, 540, 551, 
                                    421, 516, 529, 552, 438, 420, 525, 408, 416, 433, 427, 490, 
                                    528, 407, 544, 523, 470, 598, 596, 597, 599, 432, 601, 428, 
                                    499, 588, 609, 403, 498, 514, 524, 423, 532, 414, 578, 443, 
                                    486, 515, 406, 531, 573, 456, 455, 459, 543, 422, 583, 482, 
                                    478, 605, 554, 530, 418, 572, 582, 413, 440, 536, 575, 534, 
                                    567, 535, 576, 412, 402, 542, 603, 475, 586, 557, 424, 504, 
                                    495, 539, 483, 508, 500, 444, 556, 429, 411, 541, 559, 415, 
                                    561, 480, 560, 562, 506, 565, 451, 434, 558, 494, 555, 502, 
                                    477, 503, 579, 400, 404, 489, 505, 479, 442, 458 }; 
     
    for i, v in pairs(interactableVehicles) do 
        if (v == getElementModel(vehicle)) then 
            return "true"; 
        end 
    end   
end 
  
  
bindKey(interactButton, "down", 
function() 
    if (lookAtVehicle) and (getElementType(lookAtVehicle) == "vehicle") and (getVehicleRelatedPosition()) then 
        local checkDoor = getVehicleDoorState(lookAtVehicle, getVehicleRelatedPosition()); 
        if (checkDoor ~= 4 ) then 
            if not(isVehicleLocked(lookAtVehicle)) then 
                triggerServerEvent("onInteractVehicleDoor", player, tonumber(getVehicleRelatedPosition())); 
            end 
        end 
    end 
end); 
  
  
function onTrunkOpened() 
    if (isDebug == "true") then 
        outputChatBox("Trunk was opened."); 
    end 
end 
addEvent("onTrunkOpened", true); 
addEventHandler("onTrunkOpened", root, onTrunkOpened); 
  
function onTrunkClosed() 
    if (isDebug == "true") then 
        outputChatBox("Trunk was closed."); 
    end 
end 
addEvent("onTrunkClosed", true); 
addEventHandler("onTrunkClosed", root, onTrunkClosed); 

Server-sided

local root = getRootElement(); 
  
  
function doServerInteractions(door) 
    lookAtVehicle = getPedTarget(source); 
    local player = source; 
     
    if (lookAtVehicle) and (getElementType(lookAtVehicle) == "vehicle" ) then 
        local doorRatio = getVehicleDoorOpenRatio(lookAtVehicle, door); 
        local doorStateS = getElementData(lookAtVehicle, tostring(door), true); 
         
        if not (doorStateS) then 
            setElementData(lookAtVehicle, door, "closed", true); 
        end 
         
        interactWith(player, lookAtVehicle, door); 
        setPedAnimation(source, "Ped", "CAR_open_LHS", 300, false, false, true, false);      
    end 
end 
addEvent("onInteractVehicleDoor", true) 
addEventHandler("onInteractVehicleDoor", root, doServerInteractions) 
  
  
local canInterActWith = "true"; 
  
function interactWith(source, vehicle, door) 
    local player = source; 
    doorRatio = getVehicleDoorOpenRatio(vehicle, door); 
    doorState = getElementData(vehicle, door); 
  
    if (doorRatio <= 0) then 
        doorState = "closed"; 
    elseif (doorRatio >= 1) then 
        doorState = "open"; 
    end 
     
    if (canInterActWith == "true") then  
        if (doorState == "closed") then 
            setTimer(function() 
                canInterActWith = "false"; 
                 
                if (doorRatio <= 1) then 
                    doorRatio = doorRatio + 0.1; 
                    if (doorRatio >= 1) then 
                        doorRatio = 1; 
                        setElementData(vehicle, door, "open", true); 
                        canInterActWith = "true"; 
                        triggerOpenEvents(player, door); 
                    end 
                end 
                setElementData(vehicle, door, "closed", true); 
                setVehicleDoorOpenRatio(vehicle, door, doorRatio); 
            end, 50, 11); 
             
        elseif (doorState == "open") then 
            setTimer ( function() 
                canInterActWith = "false"; 
                 
                if (doorRatio > 0) then 
                    doorRatio = doorRatio - 0.1; 
                     
                    if (doorRatio <= 0) then 
                        doorRatio = 0; 
                        setElementData(vehicle, door, "closed", true); 
                        canInterActWith = "true"; 
                        triggerCloseEvents(player, door); 
                    end                 
                end 
                setElementData(vehicle, door, "open", true); 
                setVehicleDoorOpenRatio(vehicle, door, doorRatio); 
            end, 50, 11); 
        end 
    end    
end 
  
function triggerOpenEvents(source, door) 
    if (source) and (door) then 
        if (door == 1) then 
            triggerClientEvent(source, "onTrunkOpened", root); 
        end 
    end 
end 
  
function triggerCloseEvents(source, door) 
    if (source) and (door) then 
        if (door == 1) then 
            triggerClientEvent(source, "onTrunkClosed", root); 
        end 
    end 
end 

What I want

- I want that when I press "M" the Trunk opens of the cars which are listed.

What went wrong?

- It doesn't open, the MTA server.exe doesn't say any error.

Thanks for helping already,

Greetings Billy

"If debugging is the process of removing software bugs, then programming must be the process of putting them in."

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