Jump to content

getElementsWithinColShape returning false


bradio10

Recommended Posts

Hi,

I am needing some help with this because its really confusing.

Basically, the script is when you enter the colshape, the gate opens, when you exit, it closes and its supose to check if anything (a player or a vehicle) is within the colshape as well. If there is, then its suppose to stay open, if there is nothing, then it can close.

I keep getting this error on line 46: Attempt to compare number with boolean

So I tried outputting the number of elements within each of the variable checks and it says 0 (like it should, or at least any number), but it still says that error for the if statement which is really confusing.

Code:

function createANewObject(objID, x, y, z, rotx, roty, rotz) 
    dunnewObj = createObject(objID, x, y, z, rotx, roty, rotz) 
    local name = tostring(getPlayerName(source)) 
    setTimer(function ()  
        local newcolshape = createColTube(x, y, z - 3, 10, 7)            
        setElementData(newcolshape, "col.group", "".. name) 
        setElementData(newcolshape, "col.gate", dunnewObj) 
    end, 5000, 1) 
    outputChatBox("Colshape will be created in 5 seconds, please move away from the gate!", source, 255, 255, 0) 
    setElementDoubleSided(dunnewObj, true) 
    outputChatBox("Name: " .. name) 
    exports.mysqlconfig:exec("INSERT INTO objects (plrName, serial, x, y, z, rotx, roty, rotz, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", name, getPlayerSerial(source), x, y, z, rotx, roty, rotz, objID) 
end 
addEvent("createNewObject", true) 
addEventHandler("createNewObject", root, createANewObject) 
  
addEventHandler("onColShapeHit", root,  
    function(player, matchingDimension) 
            local name = getElementData(source, "col.group") 
            if (tostring(name) == tostring(getPlayerName(player))) then 
                local gate = getElementData(source, "col.gate") 
                    if (gate) then 
                        local isOpen = getElementData(gate, "gate.open") 
                        --local plrInCol = getElementsWithinColShape(source, "player") 
                        --local vehInCol = getElementsWithinColShape(source, "vehicle") 
                        if (isOpen == false) then 
                            local gx, gy, gz = getElementPosition(gate) 
                            local gz = gz + 3 
                            moveObject(gate, 1200, gx, gy, gz, 0, 0, 0, "OutBounce") 
                            setElementData(gate, "gate.open", true) 
                        end 
                    end 
            end 
    end 
) 
  
addEventHandler("onColShapeLeave", root,  
    function(player, matchingDimension) 
        local name = getElementData(source, "col.group") 
        if (tostring(name) == tostring(getPlayerName(player))) then 
            local gate = getElementData(source, "col.gate") 
            if (gate) then 
                local plrInCol = getElementsWithinColShape(source, "player") 
                local vehInCol = getElementsWithinColShape(source, "vehicle") 
                outputChatBox("1: " .. tostring(#plrInCol) .. ", 2: " .. tostring(#vehInCol), player) 
                if (not tonumber(#plrInCol) >= 1 or tonumber(#vehInCol) >= 1) then 
                    local gx, gy, gz = getElementPosition(gate) 
                    gz = gz - 3 
                    moveObject(gate, 1200, gx, gy, gz, 0, 0, 0, "OutBounce") 
                    setElementData(gate, "gate.open", false) 
                end 
            end 
        end 
    end 
) 

The first part of the script is how the colshapes and objects are being created, just in case you needed to know that.

Any ideas?

Thanks

Link to comment

Similar problem happened to someone else some days ago.

if (not tonumber(#plrInCol) >= 1 or tonumber(#vehInCol) >= 1) then 

See here:

not tonumber(#plrInCol) 

When you do this, you are converting the value to a boolean, you need to use parentheses, like this:

if ( not ( tonumber ( #plrInCol ) >= 1 ) or tonumber(#vehInCol) >= 1) then 

Link to comment
Similar problem happened to someone else some days ago.
if (not tonumber(#plrInCol) >= 1 or tonumber(#vehInCol) >= 1) then 

See here:

not tonumber(#plrInCol) 

When you do this, you are converting the value to a boolean, you need to use parentheses, like this:

if ( not ( tonumber ( #plrInCol ) >= 1 ) or tonumber(#vehInCol) >= 1) then 

Ah, thanks.

Now, a new problem. When I go into the colshape, then walk out again, if there is a vehicle still within the colshape, it still closes, even though its suppose to stay open until the vehicle or whatever it is inside is taken away.

Have I done the if statement properly for that?

Thanks

Link to comment
function createANewObject(objID, x, y, z, rotx, roty, rotz) 
    dunnewObj = createObject(objID, x, y, z, rotx, roty, rotz) 
    local name = tostring(getPlayerName(source)) 
    setTimer(function () 
        local newcolshape = createColTube(x, y, z - 3, 10, 7)           
        setElementData(newcolshape, "col.group", "".. name) 
        setElementData(newcolshape, "col.gate", dunnewObj) 
    end, 5000, 1) 
    outputChatBox("Colshape will be created in 5 seconds, please move away from the gate!", source, 255, 255, 0) 
    setElementDoubleSided(dunnewObj, true) 
    outputChatBox("Name: " .. name) 
    exports.mysqlconfig:exec("INSERT INTO objects (plrName, serial, x, y, z, rotx, roty, rotz, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", name, getPlayerSerial(source), x, y, z, rotx, roty, rotz, objID) 
end 
addEvent("createNewObject", true) 
addEventHandler("createNewObject", root, createANewObject) 
  
addEventHandler("onColShapeHit", root, 
    function(player, matchingDimension) 
            local name = getElementData(source, "col.group") 
            if (tostring(name) == tostring(getPlayerName(player))) then 
                local gate = getElementData(source, "col.gate") 
                    if (gate) then 
                        local isOpen = getElementData(gate, "gate.open") 
                        --local plrInCol = getElementsWithinColShape(source, "player") 
                        --local vehInCol = getElementsWithinColShape(source, "vehicle") 
                        if (isOpen == false) then 
                            local gx, gy, gz = getElementPosition(gate) 
                            local gz = gz + 3 
                            moveObject(gate, 1200, gx, gy, gz, 0, 0, 0, "OutBounce") 
                            setElementData(gate, "gate.open", true) 
                        end 
                    end 
            end 
    end 
) 
  
addEventHandler("onColShapeLeave", root, 
    function(player, matchingDimension) 
        local name = getElementData(source, "col.group") 
        if (tostring(name) == tostring(getPlayerName(player))) then 
            local gate = getElementData(source, "col.gate") 
            if (gate) then 
                local plrInCol = getElementsWithinColShape(source, "player") 
                local vehInCol = getElementsWithinColShape(source, "vehicle") 
                outputChatBox("1: " .. tostring(#plrInCol) .. ", 2: " .. tostring(#vehInCol), player) 
                if (not tonumber(#plrInCol) >= 1 or tonumber(#vehInCol) >= 1) then 
                    local gx, gy, gz = getElementPosition(gate) 
                    gz = gz - 3 
                    moveObject(gate, 1200, gx, gy, gz, 0, 0, 0, "OutBounce") 
                    setElementData(gate, "gate.open", false) 
                end 
            end 
        end 
    end 
)function createANewObject(objID, x, y, z, rotx, roty, rotz) 
    dunnewObj = createObject(objID, x, y, z, rotx, roty, rotz) 
    local name = tostring(getPlayerName(source)) 
    setTimer(function () 
        local newcolshape = createColTube(x, y, z - 3, 10, 7)           
        setElementData(newcolshape, "col.group", "".. name) 
        setElementData(newcolshape, "col.gate", dunnewObj) 
    end, 5000, 1) 
    outputChatBox("Colshape will be created in 5 seconds, please move away from the gate!", source, 255, 255, 0) 
    setElementDoubleSided(dunnewObj, true) 
    outputChatBox("Name: " .. name) 
    exports.mysqlconfig:exec("INSERT INTO objects (plrName, serial, x, y, z, rotx, roty, rotz, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", name, getPlayerSerial(source), x, y, z, rotx, roty, rotz, objID) 
end 
addEvent("createNewObject", true) 
addEventHandler("createNewObject", root, createANewObject) 
  
addEventHandler("onColShapeHit", root, 
    function(player, matchingDimension) 
            local name = getElementData(source, "col.group") 
            if (tostring(name) == tostring(getPlayerName(player))) then 
                local gate = getElementData(source, "col.gate") 
                    if (gate) then 
                        local isOpen = getElementData(gate, "gate.open") 
                        --local plrInCol = getElementsWithinColShape(source, "player") 
                        --local vehInCol = getElementsWithinColShape(source, "vehicle") 
                        if (isOpen == false) then 
                            local gx, gy, gz = getElementPosition(gate) 
                            local gz = gz + 3 
                            moveObject(gate, 1200, gx, gy, gz, 0, 0, 0, "OutBounce") 
                            setElementData(gate, "gate.open", true) 
                        end 
                    end 
            end 
    end 
) 
  
addEventHandler("onColShapeLeave", root, 
    function(player, matchingDimension) 
        local name = getElementData(source, "col.group") 
        if (tostring(name) == tostring(getPlayerName(player))) then 
            local gate = getElementData(source, "col.gate") 
            if (gate) then 
                local plrInCol = getElementsWithinColShape(source, "player") 
                local vehInCol = getElementsWithinColShape(source, "vehicle") 
                outputChatBox("1: " .. tostring(#plrInCol) .. ", 2: " .. tostring(#vehInCol), player) 
                if ( #plrInCol == 0 and #vehInCol == 0 ) then 
                    local gx, gy, gz = getElementPosition(gate) 
                    gz = gz - 3 
                    moveObject(gate, 1200, gx, gy, gz, 0, 0, 0, "OutBounce") 
                    setElementData(gate, "gate.open", false) 
                end 
            end 
        end 
    end 
) 

Try that.

Link to comment
function createANewObject(objID, x, y, z, rotx, roty, rotz) 
    dunnewObj = createObject(objID, x, y, z, rotx, roty, rotz) 
    local name = tostring(getPlayerName(source)) 
    setTimer(function () 
        local newcolshape = createColTube(x, y, z - 3, 10, 7)           
        setElementData(newcolshape, "col.group", "".. name) 
        setElementData(newcolshape, "col.gate", dunnewObj) 
    end, 5000, 1) 
    outputChatBox("Colshape will be created in 5 seconds, please move away from the gate!", source, 255, 255, 0) 
    setElementDoubleSided(dunnewObj, true) 
    outputChatBox("Name: " .. name) 
    exports.mysqlconfig:exec("INSERT INTO objects (plrName, serial, x, y, z, rotx, roty, rotz, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", name, getPlayerSerial(source), x, y, z, rotx, roty, rotz, objID) 
end 
addEvent("createNewObject", true) 
addEventHandler("createNewObject", root, createANewObject) 
  
addEventHandler("onColShapeHit", root, 
    function(player, matchingDimension) 
            local name = getElementData(source, "col.group") 
            if (tostring(name) == tostring(getPlayerName(player))) then 
                local gate = getElementData(source, "col.gate") 
                    if (gate) then 
                        local isOpen = getElementData(gate, "gate.open") 
                        --local plrInCol = getElementsWithinColShape(source, "player") 
                        --local vehInCol = getElementsWithinColShape(source, "vehicle") 
                        if (isOpen == false) then 
                            local gx, gy, gz = getElementPosition(gate) 
                            local gz = gz + 3 
                            moveObject(gate, 1200, gx, gy, gz, 0, 0, 0, "OutBounce") 
                            setElementData(gate, "gate.open", true) 
                        end 
                    end 
            end 
    end 
) 
  
addEventHandler("onColShapeLeave", root, 
    function(player, matchingDimension) 
        local name = getElementData(source, "col.group") 
        if (tostring(name) == tostring(getPlayerName(player))) then 
            local gate = getElementData(source, "col.gate") 
            if (gate) then 
                local plrInCol = getElementsWithinColShape(source, "player") 
                local vehInCol = getElementsWithinColShape(source, "vehicle") 
                outputChatBox("1: " .. tostring(#plrInCol) .. ", 2: " .. tostring(#vehInCol), player) 
                if (not tonumber(#plrInCol) >= 1 or tonumber(#vehInCol) >= 1) then 
                    local gx, gy, gz = getElementPosition(gate) 
                    gz = gz - 3 
                    moveObject(gate, 1200, gx, gy, gz, 0, 0, 0, "OutBounce") 
                    setElementData(gate, "gate.open", false) 
                end 
            end 
        end 
    end 
)function createANewObject(objID, x, y, z, rotx, roty, rotz) 
    dunnewObj = createObject(objID, x, y, z, rotx, roty, rotz) 
    local name = tostring(getPlayerName(source)) 
    setTimer(function () 
        local newcolshape = createColTube(x, y, z - 3, 10, 7)           
        setElementData(newcolshape, "col.group", "".. name) 
        setElementData(newcolshape, "col.gate", dunnewObj) 
    end, 5000, 1) 
    outputChatBox("Colshape will be created in 5 seconds, please move away from the gate!", source, 255, 255, 0) 
    setElementDoubleSided(dunnewObj, true) 
    outputChatBox("Name: " .. name) 
    exports.mysqlconfig:exec("INSERT INTO objects (plrName, serial, x, y, z, rotx, roty, rotz, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", name, getPlayerSerial(source), x, y, z, rotx, roty, rotz, objID) 
end 
addEvent("createNewObject", true) 
addEventHandler("createNewObject", root, createANewObject) 
  
addEventHandler("onColShapeHit", root, 
    function(player, matchingDimension) 
            local name = getElementData(source, "col.group") 
            if (tostring(name) == tostring(getPlayerName(player))) then 
                local gate = getElementData(source, "col.gate") 
                    if (gate) then 
                        local isOpen = getElementData(gate, "gate.open") 
                        --local plrInCol = getElementsWithinColShape(source, "player") 
                        --local vehInCol = getElementsWithinColShape(source, "vehicle") 
                        if (isOpen == false) then 
                            local gx, gy, gz = getElementPosition(gate) 
                            local gz = gz + 3 
                            moveObject(gate, 1200, gx, gy, gz, 0, 0, 0, "OutBounce") 
                            setElementData(gate, "gate.open", true) 
                        end 
                    end 
            end 
    end 
) 
  
addEventHandler("onColShapeLeave", root, 
    function(player, matchingDimension) 
        local name = getElementData(source, "col.group") 
        if (tostring(name) == tostring(getPlayerName(player))) then 
            local gate = getElementData(source, "col.gate") 
            if (gate) then 
                local plrInCol = getElementsWithinColShape(source, "player") 
                local vehInCol = getElementsWithinColShape(source, "vehicle") 
                outputChatBox("1: " .. tostring(#plrInCol) .. ", 2: " .. tostring(#vehInCol), player) 
                if ( #plrInCol == 0 and #vehInCol == 0 ) then 
                    local gx, gy, gz = getElementPosition(gate) 
                    gz = gz - 3 
                    moveObject(gate, 1200, gx, gy, gz, 0, 0, 0, "OutBounce") 
                    setElementData(gate, "gate.open", false) 
                end 
            end 
        end 
    end 
) 

Try that.

One problem that I have noticed when driving into the colshape is that when I leave, it still detects a vehicle in the colshape, even though I have driven out and no other vehicle is within the colshape.

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