Sign in to follow this  
itHyperoX

destroyElement..

Recommended Posts

Hello, i'm working on a vehicle system, and again got some problem....

 

destroyElement - expected element at argument 1.

Code:

addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),function()
    targetSpawnedVeh = getElementData(source,"vehSpawned")
    if targetSpawnedVeh then
        destroyElement(targetSpawnedVeh)
        removeElementData(source,"vehSpawned")
        outputChatBox("success1")
    else
        outputChatBox("error")    
    end
end)

addEventHandler("onResourceStop",resourceRoot,function()
    targetSpawnedVeh = getElementData(source,"vehSpawned")
    if targetSpawnedVeh then
        destroyElement(targetSpawnedVeh)
        removeElementData(source,"vehSpawned")
        outputChatBox("success2")
    else
        outputChatBox("error")    
    end
end)

Always returning to "error"

Edited by TheMOG

Share this post


Link to post

When checking if the element exists, try to use if isElement(vehicle) instead of just if vehicle.

  • Like 1

Share this post


Link to post
4 hours ago, Anubhav said:

where are you setting the element data?

when player spawning the vehicle

42 minutes ago, pa3ck said:

When checking if the element exists, try to use if isElement(vehicle) instead of just if vehicle.

 Tried, the same error.

Share this post


Link to post

Can you show us how you set the elementData, because if the isElement() doesn't fail, that means there's an element but not necessarily a vehicle. 

Share this post


Link to post
                  if #playercar == 1 then
                       local x,y,z = getElementPosition(source)
                       local vehicle = createVehicle(playercar[1].vehID,x + 5,y +2,z)
                    setElementData(vehicle,"vehicle:Owner","vehicleSpawned")
                       setElementData(vehicle,"vehicleSpawned",getPlayerName(source))
                       setElementData(source, "vehicleSpawned", vehicle)
                    setElementData(vehicle,"VehOwner",getPlayerName(source))
                       vehicleSpawnMessage(1)
                   else
                    noVehicleOnSlot(1)
                end

 

I checked with a command, and its working.

 

addCommandHandler("test",function(source,cmd)
    veh = getElementData(source,"myVehicle1")
    isInVeh = getPedOccupiedVehicle ( source )
    if isInVeh then    
        if isElement(veh) then
            outputChatBox("The vehicle is spawned",source)
        else
            outputChatBox("The vehicle is not spawned",source)    
        end
    else
        outputChatBox("Not in veh")    
    end    
end)

 

Edited by TheMOG

Share this post


Link to post

You're using the events onResourceStart and onResourceStop with "source" --> source is never going to be a player, it will always be the resource. What you should do is, when a resource starts, use a for loop to loop through every player. 

  • Thanks 1

Share this post


Link to post
13 minutes ago, pa3ck said:

You're using the events onResourceStart and onResourceStop with "source" --> source is never going to be a player, it will always be the resource. What you should do is, when a resource starts, use a for loop to loop through every player. 

Niice thank you buddy. Working perfect

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.