Jump to content

[Q]Using same command for toggling?


joedajoester

Recommended Posts

Hi, so im trying to have the command "spatula" attach objects (which it does) and then when i type "spatula" again i want it to remove the objects only if they are attached. Spent a few hours on this but i get no error and only the removing part doesnt work.

function addspatula (player) 
local spatula = 0 
  if spatula == 0 then 
car1gate1 = createObject(971, 0, 0, 5) 
car1gate2 = createObject(971, 0, 0, 5) 
car2gate1 = createObject(971, 0, 0, 5) 
car2gate2 = createObject(971, 0, 0, 5) 
     vehicle = getPedOccupiedVehicle(player) 
attachElements( car1gate1, vehicle, 0.4, 6.2, -0.6, 90, 90, 90 ) 
attachElements( car1gate2, vehicle, 0.4, 13.2, -0.6, 90, 90, 90 ) 
addVehicleUpgrade ( vehicle, 1087 ) 
addVehicleUpgrade ( vehicle, 1087 ) 
  else 
removeElements( car1gate1, vehicle, 0.4, 6.2, -0.6, 90, 90, 90 ) 
removeElements( car1gate2, vehicle, 0.4, 13.2, -0.6, 90, 90, 90 ) 
  end 
end 
  
addCommandHandler("spatula", addspatula) 
  

Link to comment

Here try this:

local gates = { 
    car1gate1 = createObject(971, 0, 0, 5), 
    car1gate2 = createObject(971, 0, 0, 5), 
    car2gate1 = createObject(971, 0, 0, 5), 
    car2gate2 = createObject(971, 0, 0, 5) 
} 
function addspatula (player) 
    local vehicle = getPedOccupiedVehicle(player) 
    if not vehicle then 
        return outputChatBox("You need to be in a vehicle!",player) 
    end 
    local spatula = getElementData(player,"spatula") or false 
    if spatula == false then 
        attachElements( gates.car1gate1, vehicle, 0.4, 6.2, -0.6, 90, 90, 90 ) 
        attachElements( gates.car1gate2, vehicle, 0.4, 13.2, -0.6, 90, 90, 90 ) 
        addVehicleUpgrade ( vehicle, 1087 ) 
        addVehicleUpgrade ( vehicle, 1087 ) 
    else 
        removeElements( gates.car1gate1, vehicle, 0.4, 6.2, -0.6, 90, 90, 90 ) 
        removeElements( gates.car1gate2, vehicle, 0.4, 13.2, -0.6, 90, 90, 90 ) 
    end 
    spatula = not spatula 
    setElementData(player,"spatula",spatula) 
end 
addCommandHandler("spatula", addspatula) 

Basically, you forgot to change the variable value. I put comments in there for you :)

Edited by Guest
Link to comment
Here try this:
function addspatula (player) 
    local spatula = 0 
    if spatula == 0 then 
        car1gate1 = createObject(971, 0, 0, 5) 
        car1gate2 = createObject(971, 0, 0, 5) 
        car2gate1 = createObject(971, 0, 0, 5) 
        car2gate2 = createObject(971, 0, 0, 5) 
        vehicle = getPedOccupiedVehicle(player) 
        attachElements( car1gate1, vehicle, 0.4, 6.2, -0.6, 90, 90, 90 ) 
        attachElements( car1gate2, vehicle, 0.4, 13.2, -0.6, 90, 90, 90 ) 
        addVehicleUpgrade ( vehicle, 1087 ) 
        addVehicleUpgrade ( vehicle, 1087 ) 
        spatula = 1 --set it to something other than 0 so it could detach 
    else 
        removeElements( car1gate1, vehicle, 0.4, 6.2, -0.6, 90, 90, 90 ) 
        removeElements( car1gate2, vehicle, 0.4, 13.2, -0.6, 90, 90, 90 ) 
        spatula = 0 --Set it to 0 for attaching again 
    end 
end 
addCommandHandler("spatula", addspatula) 

Basically, you forgot to change the variable value. I put comments in there for you :)

Thanks for the quick reply, however it still doesn't remove the objects

Link to comment

i think removeElements must be

destroyElement 

there's no such thing as removeElements. Try replacing the removelements function with this:

destroyElement( car1gate1, vehicle, 0.4, 6.2, -0.6, 90, 90, 90 ) 
destroyElement( car1gate2, vehicle, 0.4, 13.2, -0.6, 90, 90, 90 ) 

Link to comment

Yea, I keep missing simple information like that...

local gates = { 
    car1gate1 = createObject(971, 0, 0, 5), 
    car1gate2 = createObject(971, 0, 0, 5), 
    car2gate1 = createObject(971, 0, 0, 5), 
    car2gate2 = createObject(971, 0, 0, 5) 
} 
function addspatula (player) 
    local vehicle = getPedOccupiedVehicle(player) 
    if not vehicle then 
        return outputChatBox("You need to be in a vehicle!",player) 
    end 
    local spatula = getElementData(player,"spatula") or false 
    if spatula == false then 
        attachElements( gates.car1gate1, vehicle, 0.4, 6.2, -0.6, 90, 90, 90 ) 
        attachElements( gates.car1gate2, vehicle, 0.4, 13.2, -0.6, 90, 90, 90 ) 
        addVehicleUpgrade ( vehicle, 1087 ) 
        addVehicleUpgrade ( vehicle, 1087 ) 
    else 
        detachElements( gates.car1gate1, vehicle) 
        detachElements( gates.car1gate2, vehicle) 
    end 
    spatula = not spatula 
    setElementData(player,"spatula",spatula) 
end 
addCommandHandler("spatula", addspatula) 

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