Jump to content

[HELP] If state is 1 then close the gate?


..:D&G:..

Recommended Posts

Hey, I tried to make something like a state, which means that if the state == 1 then move the gate to possition A, and if the state == 0 then move the gate to possition B.

local state = 0 
local distance = 50 
  
function openGarageGate(thePlayer, cmd, garageID, state) 
    if not garageID then 
        outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) 
    else 
        local garageID = tonumber(garageID) 
        local found = false 
        for i, v in ipairs (getElementsByType("object")) do 
            if (getElementModel(v) == 17950) then 
                local gate = getElementData(v, "poarta") 
                local dbid = tonumber(getElementData(v, "dbid")) 
                if (dbid == garageID) then 
                    local px,py,pz = getElementPosition(thePlayer) 
                    local ox,oy,oz = getElementPosition(v) 
                    if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then 
                        if (state == 0) then 
                            local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                            setElementAttachedOffsets(gate, gateX, gateY-1, gateZ+1.65, rotX, -90, rotZ) 
                            state = true 
                            outputChatBox("You opened the garage!", thePlayer, 0, 255, 0) 
                            found = true 
                            break 
                        elseif (state == 1) then 
                                local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                                setElementAttachedOffsets(gate, gateX, gateY+1, gateZ-1.65, rotX, 0, rotZ) 
                                outputChatBox("You closed the garage!", thePlayer, 0, 255, 0) 
                            end 
                        end 
                    else     
                        outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) 
                    end 
                end 
            end 
        end 
        if found == false then 
        outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) 
        end 
    end 
end 
addCommandHandler("opengarage", openGarageGate) 

For some reason it says that there was no garage found with that ID...

Link to comment

Try this :

local state = 0 
local distance = 50 
  
function openGarageGate(thePlayer, cmd, garageID, state) 
    if not garageID then  
        outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) 
    else 
        local garageID = tonumber(garageID) 
        local found = false 
        for i, v in ipairs (getElementsByType("object")) do 
            if (getElementModel(v) == 17950) then 
                local gate = getElementData(v, "poarta") 
                local dbid = tonumber(getElementData(v, "dbid")) 
                if (dbid == garageID) then 
                    local px,py,pz = getElementPosition(thePlayer) 
                    local ox,oy,oz = getElementPosition(v) 
                    if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then 
                        if (state == 0) then 
                            local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                            setElementAttachedOffsets(gate, gateX, gateY-1, gateZ+1.65, rotX, -90, rotZ) 
                            state = 1 
                            outputChatBox("You opened the garage!", thePlayer, 0, 255, 0) 
                            found = true 
                            break 
                        elseif (state == 1) then 
                                local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                                setElementAttachedOffsets(gate, gateX, gateY+1, gateZ-1.65, rotX, 0, rotZ) 
                                outputChatBox("You closed the garage!", thePlayer, 0, 255, 0) 
                            end 
                        end 
                    else    
                        outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) 
                    end 
                end 
            end 
        end 
        if found == false then 
        outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) 
        end 
    end 
end 
addCommandHandler("opengarage", openGarageGate) 

Link to comment

[quote name=..:D&G:..]It says that I am too far away from the garage

ok

local state = 0 
local distance = 50 
  
function openGarageGate(thePlayer, cmd, garageID, state) 
    if not garageID then  
        outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) 
    else 
        local garageID = tonumber(garageID) 
        local found = false 
        for i, v in ipairs (getElementsByType("object")) do 
            if (getElementModel(v) == 17950) then 
                local gate = getElementData(v, "poarta") 
                local dbid = tonumber(getElementData(v, "dbid")) 
                if (dbid == garageID) then 
                    local px,py,pz = getElementPosition(thePlayer) 
                    local ox,oy,oz = getElementPosition(v) 
                    if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then 
                        if (state == 0) then 
                            local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                            setElementAttachedOffsets(gate, gateX, gateY-1, gateZ+1.65, rotX, -90, rotZ) 
                            state = 1 
                            outputChatBox("You opened the garage!", thePlayer, 0, 255, 0) 
                            found = true 
                            break 
                        elseif (state == 1) then 
                                local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                                setElementAttachedOffsets(gate, gateX, gateY+1, gateZ-1.65, rotX, 0, rotZ) 
                                outputChatBox("You closed the garage!", thePlayer, 0, 255, 0) 
                            end 
                             else    
                        outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) 
                        end 
                    end 
                end 
            end 
        end 
        if found == false then 
        outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) 
    end 
end 
addCommandHandler("opengarage", openGarageGate) 

Link to comment

Um.. I got another problem xD If I type in the command 3 times, it workes the first 2 times (it says I open the garage and then close it) but the other times it keeps saying I close the garage and this like is executed:

setElementAttachedOffsets(gate, gateX, gateY+1, gateZ-1.65, rotX, 0, rotZ) 

Any ideas why?

Link to comment

Done

local state = 0 
local distance = 50 
  
function openGarageGate(thePlayer, cmd, garageID, state) 
    if not garageID then 
        outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) 
    else 
        local garageID = tonumber(garageID) 
        for i, v in ipairs (getElementsByType("object")) do 
            if (getElementModel(v) == 17950) then 
                local gate = getElementData(v, "poarta") 
                local dbid = tonumber(getElementData(v, "dbid")) 
                if (dbid == garageID) then 
                    local px,py,pz = getElementPosition(thePlayer) 
                    local ox,oy,oz = getElementPosition(v) 
                    if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then 
                        if (state == 0) then 
                            local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                            setElementAttachedOffsets(gate, gateX, gateY-1, gateZ+1.65, rotX, -90, rotZ) 
                            outputChatBox("You opened the garage!", thePlayer, 0, 255, 0) 
                            state = 1 
                        elseif (state == 1) then 
                                local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                                setElementAttachedOffsets(gate, gateX, gateY+1, gateZ-1.65, rotX, 0, rotZ) 
                                outputChatBox("You closed the garage!", thePlayer, 0, 255, 0) 
                                state = 0 
                            end 
                             else   
                        outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) 
                        end 
                        else 
                    outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) 
                end 
            end 
        end 
    end 
end 
addCommandHandler("opengarage", openGarageGate) 

Link to comment

It workes but I get this:

There's no garage with this ID! 
There's no garage with this ID! 
There's no garage with this ID! 
There's no garage with this ID! 
There's no garage with this ID! 
There's no garage with this ID! 
There's no garage with this ID! 
There's no garage with this ID! 
There's no garage with this ID! 
There's no garage with this ID! 
You closed the garage! 

And when I am far away from the garage, it keeps saying "there's no garage with this ID" like about 10 times and then it says that I am too far away...

local state = 0 
local distance = 50 
  
function openGarageGate(thePlayer, garageID) 
    local garageID = tonumber(garageID) 
    local found = false 
    for i, v in ipairs (getElementsByType("object")) do 
        if (getElementModel(v) == 17950) then 
            local gate = getElementData(v, "poarta") 
            local dbid = tonumber(getElementData(v, "dbid")) 
            if (dbid == garageID) then 
                local px,py,pz = getElementPosition(thePlayer) 
                local ox,oy,oz = getElementPosition(v) 
                if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then 
                    if (state == 0) then 
                        local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                        setElementAttachedOffsets(gate, gateX, gateY-1, gateZ+1.65, rotX, -90, rotZ) 
                        outputChatBox("You opened the garage!", thePlayer, 0, 255, 0) 
                        state = 1 
                    elseif (state == 1) then 
                        local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                        setElementAttachedOffsets(gate, gateX, gateY+1, gateZ-1.65, rotX, 0, rotZ) 
                        outputChatBox("You closed the garage!", thePlayer, 0, 255, 0) 
                        state = 0 
                    end 
                else   
                    outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) 
                end 
            else 
                outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) 
            end 
        end 
    end 
end 

Link to comment

Ok try this

local state = 0 
local distance = 50 
  
function openGarageGate(thePlayer, garageID) 
    local garageID = tonumber(garageID) 
    local found = false 
        for i, v in ipairs (getElementsByType("object")) do 
            if (getElementModel(v) == 17950) then 
            local gate = getElementData(v, "poarta") 
            local dbid = tonumber(getElementData(v, "dbid")) 
                if (dbid == garageID) then 
                    found = true 
                break 
            local px,py,pz = getElementPosition(thePlayer) 
        local ox,oy,oz = getElementPosition(v) 
    if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then 
        local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
            if (state == 0) then 
                setElementAttachedOffsets(gate, gateX, gateY-1, gateZ+1.65, rotX, -90, rotZ) 
                    outputChatBox("You opened the garage!", thePlayer, 0, 255, 0) 
                state = 1 
            elseif (state == 1) then 
        setElementAttachedOffsets(gate, gateX, gateY+1, gateZ-1.65, rotX, 0, rotZ) 
    outputChatBox("You closed the garage!", thePlayer, 0, 255, 0) 
        state = 0 
        end 
    else   
        outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) 
        end 
            end 
        end 
    end 
    if found == false then 
        outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) 
    end 
end 

Link to comment

It just spams "you are too far away from the garage"

function openGarageGate(thePlayer, garageID) 
    local garageID = tonumber(garageID) 
    local found = false 
        for i, v in ipairs (getElementsByType("object")) do 
            if (getElementModel(v) == 17950) then 
            local gate = getElementData(v, "poarta") 
            local dbid = tonumber(getElementData(v, "dbid")) 
                if (dbid == garageID) then 
                    found = true 
                    break 
                end 
                local px,py,pz = getElementPosition(thePlayer) 
                local ox,oy,oz = getElementPosition(v) 
            if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then 
                local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                if (state == 0) then 
                    setElementAttachedOffsets(gate, gateX, gateY-1, gateZ+1.65, rotX, -90, rotZ) 
                    outputChatBox("You opened the garage!", thePlayer, 0, 255, 0) 
                    state = 1 
            elseif (state == 1) then 
                setElementAttachedOffsets(gate, gateX, gateY+1, gateZ-1.65, rotX, 0, rotZ) 
                outputChatBox("You closed the garage!", thePlayer, 0, 255, 0) 
                state = 0 
            end 
        end 
    else   
        outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) 
    end 
end 
    if found == false then 
        outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) 
    end 
end 

Link to comment

Are u sure that You didn't edit the code:

Try this one

local state = 0 
local distance = 50 
  
function openGarageGate(thePlayer, cmd, garageID, state) 
    if not garageID then 
        outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) 
    else 
        local garageID = tonumber(garageID) 
        local found = false 
        for i, v in ipairs (getElementsByType("object")) do 
            if (getElementModel(v) == 17950) then 
                local gate = getElementData(v, "poarta") 
                local dbid = tonumber(getElementData(v, "dbid")) 
                if (dbid == garageID) then 
                    local px,py,pz = getElementPosition(thePlayer) 
                    local ox,oy,oz = getElementPosition(v) 
                    if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then 
                        if (state == 0) then 
                            local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                            setElementAttachedOffsets(gate, gateX, gateY-1, gateZ+1.65, rotX, -90, rotZ) 
                            state = 1 
                            outputChatBox("You opened the garage!", thePlayer, 0, 255, 0) 
                            found = true 
                            break 
                        elseif (state == 1) then 
                                local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) 
                                setElementAttachedOffsets(gate, gateX, gateY+1, gateZ-1.65, rotX, 0, rotZ) 
                                state = 0 
                                outputChatBox("You closed the garage!", thePlayer, 0, 255, 0) 
                            end 
                             else   
                        outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) 
                        end 
                    end 
                end 
            end 
        end 
        if found == false then 
        outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) 
    end 
end 
addCommandHandler("opengarage", openGarageGate) 

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