Jump to content

Creating object need help


isa_Khamdan

Recommended Posts

Hello ,

I want your help to edit this code to make it start with a command not automatically else I want it that if you type the command again and the object is already created it will destroy it

-- createObject 
local x,y,z = getElementPosition(Vehicle) 
local marker = createObject ( 1318, x,y,z+2, 0, 0, 0 ) 
  
addEventHandler("onVehicleStartEnter",resourceRoot, 
function () 
attachElements (marker,Vehicle,0,0,2) 
end 
) 

Link to comment
  
addCommandHandler("obj", 
    function () 
        if ( getPedOccupiedVehicle ( source ) ) then 
            if ( isElement ( marker ) ) then 
                destroyElement ( marker ); 
            else 
                local x,y,z = getElementPosition( getPedOccupiedVehicle( source ) ); 
                local marker = createObject ( 1318, x,y,z+2, 0, 0, 0 ); 
                attachElements (marker, getPedOccupiedVehicle( source ) , 0, 0, 2 ); 
            end 
        else 
            outputChatBox("You need to be in a vehicle to do this.", source); 
        end 
    end 
); 

Like that? ._.'

Link to comment
addEventHandler("onVehicleStartEnter",resourceRoot, 

.... You cannot exacly drive get into the resource... lol

Use this:

-- createObject 
local x,y,z = getElementPosition(Vehicle) 
local marker = createObject ( 1318, x,y,z+2, 0, 0, 0 ) 
  
addEventHandler("onVehicleStartEnter", Vehicle, function () 
    attachElements (marker,Vehicle,0,0,2) 
end ) 

Link to comment
addEventHandler("onVehicleStartEnter",resourceRoot, 

.... You cannot exacly drive get into the resource... lol

Use this:

-- createObject 
local x,y,z = getElementPosition(Vehicle) 
local marker = createObject ( 1318, x,y,z+2, 0, 0, 0 ) 
  
addEventHandler("onVehicleStartEnter", Vehicle, function () 
    attachElements (marker,Vehicle,0,0,2) 
end ) 

Vehicle is not defined,

use getPedOccupiedVehicle for define it.

Link to comment
addEventHandler("onVehicleStartEnter",resourceRoot, 

.... You cannot exacly drive get into the resource... lol

Use this:

-- createObject 
local x,y,z = getElementPosition(Vehicle) 
local marker = createObject ( 1318, x,y,z+2, 0, 0, 0 ) 
  
addEventHandler("onVehicleStartEnter", Vehicle, function () 
    attachElements (marker,Vehicle,0,0,2) 
end ) 

Vehicle is not defined,

use getPedOccupiedVehicle for define it.

It doesn't work , that's the whole code

local C = createVehicle ( 547, 2506.1220, -1680.1697, 13.5765 )  
    setVehiclePaintjob ( C, 2 ) 
addEventHandler("onResourceStart",resourceRoot, 
function() 
    setVehicleDamageProof(C,true) 
  
addCommandHandler("obj", 
  
    function () 
  
        if (source==C) then 
  
            if ( isElement ( marker ) ) then 
  
                destroyElement ( marker ); 
  
            else 
  
                local x,y,z = getElementPosition(C) 
  
                local marker = createObject ( 1318, x,y,z+2, 0, 0, 0 ); 
  
                attachElements (marker,C, 0, 0, 2 ); 
  
            end 
  
        else 
  
            outputChatBox("You need to be in a vehicle to do this.", source); 
  
        end 
  
    end 
  
); 

Link to comment
local C = createVehicle ( 547, 2506.1220, -1680.1697, 13.5765 ) 
setVehiclePaintjob ( C, 2 ) 
addEventHandler("onResourceStart",resourceRoot, 
function() 
    setVehicleDamageProof(C,true) 
end 
); 
  
addCommandHandler("obj",  
    function ()  
        if (source==C) then  
            if ( isElement ( marker ) ) then  
                destroyElement ( marker );  
            else  
                local x,y,z = getElementPosition(C)  
                local marker = createObject ( 1318, x,y,z+2, 0, 0, 0 );  
                attachElements (marker,C, 0, 0, 2 );  
            end  
        else 
            outputChatBox("You need to be in a vehicle to do this.", source); 
         end 
     end 
 ); 

You forgot to close your function on line 4.

Link to comment
local C = createVehicle ( 547, 2506.1220, -1680.1697, 13.5765 ) 
setVehiclePaintjob ( C, 2 ) 
addEventHandler("onResourceStart",resourceRoot, 
function() 
    setVehicleDamageProof(C,true) 
end 
); 
  
addCommandHandler("obj",  
    function ()  
        if (source==C) then  
            if ( isElement ( marker ) ) then  
                destroyElement ( marker );  
            else  
                local x,y,z = getElementPosition(C)  
                local marker = createObject ( 1318, x,y,z+2, 0, 0, 0 );  
                attachElements (marker,C, 0, 0, 2 );  
            end  
        else 
            outputChatBox("You need to be in a vehicle to do this.", source); 
         end 
     end 
 ); 

You forgot to close your function on line 4.

It keep repeating the outputChatbox message and it didn't create the object and attach it to the vehicle

Link to comment

Yeah but wtf, the source of a command ins't a vehicle... try this;

local C = createVehicle ( 547, 2506.1220, -1680.1697, 13.5765 ) 
setVehiclePaintjob ( C, 2 ); 
addEventHandler("onResourceStart",resourceRoot, 
    function() 
        setVehicleDamageProof( C, true ); 
        setElementData( c, "eventVehicle", true ); 
    end 
); 
  
addCommandHandler("obj",  
    function (thePlayer, command )  
        if not ( isPedInVehicle ( thePlayer ) ) then outputChatBox("You're not in a vehicle.", source); return; end 
        if ( getElementData( getPedOccupiedVehicle( thePlayer ), "eventVehicle" ) ) then 
            if ( isElement ( marker ) ) then  
                destroyElement ( marker );  
            else  
                local x,y,z = getElementPosition(C)  
                local marker = createObject ( 1318, x,y,z+2, 0, 0, 0 );  
                attachElements (marker,C, 0, 0, 2 );  
            end  
        else 
            outputChatBox("You're in the wrong vehicle.", source); 
         end 
     end 
 ); 

try that.

Link to comment
local C = createVehicle ( 547, 2506.1220, -1680.1697, 13.5765 ) 
setVehiclePaintjob ( C, 2 ) 
setVehicleDamageProof ( C, true ) 
setElementData ( C, "eventVehicle", true ) 
  
addCommandHandler ( "obj", 
    function ( thePlayer ) 
        if ( not isPedInVehicle ( thePlayer ) ) then 
            outputChatBox ( "You're not in a vehicle.", thePlayer ) 
            return 
        end 
  
        if getElementData ( getPedOccupiedVehicle ( thePlayer ), "eventVehicle" ) then 
            if ( isElement ( marker ) ) then 
                destroyElement ( marker ) 
            else 
                local x, y, z = getElementPosition ( C ) 
                marker = createObject ( 1318, x, y, z + 2, 0, 0, 0 ) 
                attachElements ( marker, C, 0, 0, 2 ) 
            end 
        else 
            outputChatBox ( "You're in the wrong vehicle.", thePlayer ) 
        end 
    end 
) 

Link to comment
local C = createVehicle ( 547, 2506.1220, -1680.1697, 13.5765 ) 
setVehiclePaintjob ( C, 2 ) 
setVehicleDamageProof ( C, true ) 
setElementData ( C, "eventVehicle", true ) 
  
addCommandHandler ( "obj", 
    function ( thePlayer ) 
        if ( not isPedInVehicle ( thePlayer ) ) then 
            outputChatBox ( "You're not in a vehicle.", thePlayer ) 
            return 
        end 
  
        if getElementData ( getPedOccupiedVehicle ( thePlayer ), "eventVehicle" ) then 
            if ( isElement ( marker ) ) then 
                destroyElement ( marker ) 
            else 
                local x,y,z = getElementPosition ( C ) 
                local marker = createObject ( 1318, x, y, z + 2, 0, 0, 0 ) 
                attachElements ( marker, C, 0, 0, 2 ) 
            end 
        else 
            outputChatBox ( "You're in the wrong vehicle.", thePlayer ) 
        end 
    end 
) 

ohh wait it works but it don't destroy the element when you use the command again?

Link to comment
I found the problem, copy it again.

Works now :D

is it possible to make this function to work with a command?

local timers = {} 
local objects = {} 
  
function getElementSpeed(element,unit) 
    if (unit == nil) then unit = 0 end 
    if (isElement(element)) then 
        local x,y,z = getElementVelocity(element) 
        if (unit=="mph" or unit==1 or unit =='1') then 
            return (x^2 + y^2 + z^2) ^ 0.5 * 100 
        else 
            return (x^2 + y^2 + z^2) ^ 0.5 * 1.61 * 100 
        end 
    else 
        outputDebugString("Not an element. Can't get speed") 
        return false 
    end 
end 
  
function D5(vehicle, player) 
    local Speed = getElementSpeed(vehicle,"kmh") 
    if Speed >= 150 then 
        local x,y,z = getElementPosition(vehicle) 
        if isElement(objects[player]) then return end 
        objects[player] = createObject(2780, x, y, z-2, 0, 0, 0) 
        setElementAlpha(objects[player], 0.0) 
        attachElements(objects[player], vehicle, 0, -2, -1) 
    else 
        if isElement(objects[player]) then 
            destroyElement(objects[player]) 
            objects[player] = nil 
        end 
    end 
end 
  
addEventHandler("onVehicleEnter", C, 
    function(thePlayer, seat) 
        if seat == 0 then 
            timers[thePlayer] = setTimer(D5, 1000, 0, source, thePlayer) 
        end 
    end 
) 
  
addEventHandler("onVehicleExit", C, 
    function(thePlayer, seat) 
        if seat == 0 then 
            if isTimer(timers[source]) then 
                killTimer(timers[source]) 
                timers[source] = nil 
                if isElement(objects[source]) then 
                    destroyElement(objects[source]) 
                    objects[source] = nil 
                end 
            end 
        end 
    end 
) 
  
addEventHandler("onPlayerQuit", root, 
    function() 
        if isTimer(timers[source]) then 
            killTimer(timers[source]) 
            timers[source] = nil 
            if isElement(objects[source]) then 
                destroyElement(objects[source]) 
                objects[source] = nil 
            end 
        end 
    end 
) 

Link to comment
Why would you add a command to the funciton that gets the speed?

Because if I add command for the one that create element it will turn it on and if I type it again it will turn it off and the get speed function will be working and it will show errors because it can't get the speed from an element that doesn't exists

Link to comment

Solidsnake14 , I found a problem in the code you gave me.

The problem is that if you are not in a vehicle or in another vehicle it will keep spamming the chat with messages

( wrong vehicle , you must be in vehicle ) even if you didn't use the command :S

local C = createVehicle ( 547, 2506.1220, -1680.1697, 13.5765 ) 
  
setVehiclePaintjob ( C, 2 ) 
  
setVehicleDamageProof ( C, true ) 
  
setElementData ( C, "eventVehicle", true ) 
  
  
  
addCommandHandler ( "obj", 
  
    function ( thePlayer ) 
  
        if ( not isPedInVehicle ( thePlayer ) ) then 
  
            outputChatBox ( "You're not in a vehicle.", thePlayer ) 
  
            return 
  
        end 
  
  
  
        if getElementData ( getPedOccupiedVehicle ( thePlayer ), "eventVehicle" ) then 
  
            if ( isElement ( marker ) ) then 
  
                destroyElement ( marker ) 
  
            else 
  
                local x, y, z = getElementPosition ( C ) 
  
                marker = createObject ( 1318, x, y, z + 2, 0, 0, 0 ) 
  
                attachElements ( marker, C, 0, 0, 2 ) 
  
            end 
  
        else 
  
            outputChatBox ( "You're in the wrong vehicle.", thePlayer ) 
  
        end 
  
    end 
  
) 

Link to comment
The function won't execute if you didn't use the command.

Yeah my mistake , but there still one problem I put this code for many players in my server and if someone do the command it will create/destroy the element in all vehicles so how can I fix this?

Edit : Never mind I found a way to avoid this :D Thanks for your help Solidsnake

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