Jump to content

attempt to index global 'dispatchTable'


mg20023

Recommended Posts

Posted

I've had this problem for a long time now and I can't seem to figure it out.

The error I receive is: c_dispatch.lua:84: attempt to index global 'dispatchTable' (a nil value)

Help would be appreciated :)

DispatchUI = { 
    label = {},
    edit = {},
    button = {},
    window = {},
    gridlist = {},
    combobox = {},
    checkbox = {}
}
cooldownDispatch = false
function cooldownDispatchT()
    cooldownDispatch = false
end
 
function dispatchGUI()
    -- to prevent double
    if isElement(DispatchUI.window[1]) then return end
 
    -- to center the gui window
    local screenX, screenY = guiGetScreenSize()
    local width = 410
    local height = 299
    local x = ( screenX - width ) / 2
    local y = ( screenY - height ) / 2
 
    -- to avoid using the function all the time
    local localPlayer = getLocalPlayer()
 
    -- multifactions won't play well with this
    if exports.factions:isPlayerInFaction(localPlayer, 59) and exports.factions:isPlayerInFaction(localPlayer, 1) then
        outputChatBox("You can only be in one law faction at a time to use /dispatch.", 255, 0, 0)
        return
    end
 
    -- so you can type freely
    guiSetInputMode("no_binds_when_editing")
 
    -- getting user's name
    local name = getPlayerName(localPlayer)
    local processedName = string.gsub(name, "_", " ")
    local isFactionLeader = exports.factions:isPlayerFactionLeader(localPlayer)
 
    -- getting the table
    local dispatchTable = getElementData(root, "dispatch:table") or { }
 
    -- building the gui elements
    DispatchUI.window[1] = guiCreateWindow(x, y, width, height, "Remote Dispatch Device - Powered by Tree Technology", false)
    guiWindowSetSizable(DispatchUI.window[1], false)
    DispatchUI.gridlist[1] = guiCreateGridList(9, 23, 392, 177, false, DispatchUI.window[1])
    guiGridListAddColumn(DispatchUI.gridlist[1], "Callsign", 0.3)
    guiGridListAddColumn(DispatchUI.gridlist[1], "Full name", 0.3)
    guiGridListAddColumn(DispatchUI.gridlist[1], "Availability", 0.3)
    DispatchUI.label[1] = guiCreateLabel(11, 206, 40, 19, "Status:", false, DispatchUI.window[1])
    DispatchUI.label[2] = guiCreateLabel(11, 234, 47, 19, "Call sign:", false, DispatchUI.window[1])
    DispatchUI.label[3] = guiCreateLabel(11, 261, 61, 19, "Availability:", false, DispatchUI.window[1])
    DispatchUI.button[1] = guiCreateButton(302, 206, 78, 45, "Close", false, DispatchUI.window[1])
    DispatchUI.button[2] = guiCreateButton(214, 206, 78, 45, "Update", false, DispatchUI.window[1])
    DispatchUI.combobox[1] = guiCreateComboBox(74, 205, 134, 100, "", false, DispatchUI.window[1])
    guiComboBoxAddItem(DispatchUI.combobox[1], "On duty")
    guiComboBoxAddItem(DispatchUI.combobox[1], "Off duty")
    DispatchUI.edit[1] = guiCreateEdit(74, 230, 134, 27, "", false, DispatchUI.window[1])
    DispatchUI.edit[2] = guiCreateEdit(74, 261, 134, 27, "", false, DispatchUI.window[1])
    DispatchUI.checkbox[1] = guiCreateCheckBox( 239, 253, 150, 16, "Joint Operations", getElementData(root, "dispatch:"), false, DispatchUI.window[1] )
    DispatchUI.label[4] = guiCreateLabel(275, 270, 47, 19, "", false, DispatchUI.window[1])
 
    -- Setting the operations
    getJointOperation()
    if not isFactionLeader then
        guiSetEnabled(DispatchUI.checkbox[1], false)
    end
 
    -- displaying the table in gridlist
    if dispatchTable then
        for k, v in pairs(dispatchTable) do
                local row = guiGridListAddRow(DispatchUI.gridlist[1])
                guiGridListSetItemText(DispatchUI.gridlist[1], row, 1, v["callsign"], false, false)
                guiGridListSetItemText(DispatchUI.gridlist[1], row, 2, k, false, false)
                guiGridListSetItemText(DispatchUI.gridlist[1], row, 3, v["availability"], false, false)
            end
        end
    end
 
    -- displaying current status for localPlayer, if any
    if type(dispatchTable[processedName]) == "table" then
        guiComboBoxSetSelected(DispatchUI.combobox[1], 0)
        guiSetText(DispatchUI.edit[1], dispatchTable[processedName]["callsign"])
        guiSetText(DispatchUI.edit[2], dispatchTable[processedName]["availability"])
    else
        guiComboBoxSetSelected(DispatchUI.combobox[1], 1)
    end
 
    -- close button
    addEventHandler("onClientGUIClick", DispatchUI.button[1], function ()
            if isElement(DispatchUI.window[1]) then
                destroyElement(DispatchUI.window[1])
                guiSetInputMode("allow_binds")
            end
        end, false)
 
    -- Joint
    addEventHandler("onClientGUIClick", DispatchUI.checkbox[1], function()
            if isFactionLeader then
                if guiCheckBoxGetSelected( DispatchUI.checkbox[1] ) then
                    setElementData(root, "dispatch:", true)
                    if (not getElementData(root, "dispatch:joint")) and getElementData(root, "dispatch:1") and getElementData(root, "dispatch:59") then
                        setElementData(root, "dispatch:joint", true)
                    end
                else
                    setElementData(root, "dispatch:", false)
                    if getElementData(root, "dispatch:joint") then
                        setElementData(root, "dispatch:joint", false)
                    end
                end
                getJointOperation()
            end
        end, false)
 
    -- update button
        addEventHandler("onClientGUIClick", DispatchUI.button[2])
            if cooldownDispatch then outputChatBox("Please wait!", 255, 0 ,0) return end
            local status = guiGetText( DispatchUI.combobox[1] )
 
            if status == "Off duty" then
                if dispatchTable[processedName] then
                    dispatchTable[processedName] = nil
                    setElementData(getLocalPlayer(), "dispatch:onDuty", false)
                    setElementData(root, "dispatch:table", dispatchTable)
                end
            else
                if not dispatchTable[processedName] then
                    dispatchTable[processedName] = { }
                end
                dispatchTable[processedName]["callsign"] = string.upper(guiGetText(DispatchUI.edit[1]))
                dispatchTable[processedName]["availability"] = guiGetText(DispatchUI.edit[2])
                if not getElementData(getLocalPlayer(), "dispatch:onDuty") then
                    setElementData(getLocalPlayer(), "dispatch:onDuty", true)
                end
                setElementData( getLocalPlayer(), "dispatch:callsign", string.upper(guiGetText(DispatchUI.edit[1])), true)
                setElementData(root, "dispatch:table", dispatchTable)
                end
               
            if not isTimer(cooldownDispatchTimer) then
                cooldownDispatchTimer = setTimer(cooldownDispatchT, 3000, 1)
                cooldownDispatch = true
            end
            refreshClientGUI()
 
function getJointOperation()
    if getElementData(root, "dispatch:joint") then
        guiSetText(DispatchUI.label[4], "Enabled")
        guiLabelSetColor( DispatchUI.label[4], 0, 255, 0 )
    elseif getElementData(root, "dispatch:1") or getElementData(root, "dispatch:59") then
        guiSetText(DispatchUI.label[4], "Pending")
        guiLabelSetColor( DispatchUI.label[4], 255, 255, 0 )
    else
        guiSetText(DispatchUI.label[4], "Disabled")
        guiLabelSetColor( DispatchUI.label[4], 255, 0, 0 )
    end
end
 
function canUseDispatch()
    if exports.global:hasItem(localPlayer, 177) and (exports.factions:isPlayerInFaction(localPlayer, 59) or exports.factions:isPlayerInFaction(localPlayer, 1)) then
        dispatchGUI()
    else
        outputChatBox("Dispatching through thin air does not seem to work well...", 255, 0, 0)
    end
end
addCommandHandler("dispatch", canUseDispatch)
 
function refreshClientGUI()
    if isElement(DispatchUI.window[1]) then
        destroyElement(DispatchUI.window[1])
        dispatchGUI()
    end
end
 
-- so it refreshes for all clients using it
addEventHandler("onClientElementDataChange", root,
function (dataName)
    if dataName == "dispatch:table" then
        refreshClientGUI()
    end
end )
 
-- so it removes you from the list if you change character also blip stuff
addEventHandler("onClientPlayerChangeNick", getLocalPlayer(),
function (oldNick)
    local dispatchTable = getElementData(root, "dispatch:table") or { }
    local processedName = string.gsub(oldNick, "_", " ")
    if dispatchTable[processedName] then
        dispatchTable[processedName] = nil
        setElementData(root, "dispatch:table", dispatchTable)
        refreshClientGUI()
    end
end)
 
-- so it removes you from the list if you leave the server
function removeFromDispatch(source)
    local dispatchTable = getElementData(root, "dispatch:table") or { }
    local processedName = string.gsub(getPlayerName(source), "_", " ")
    if dispatchTable[processedName] then
        dispatchTable[processedName] = nil
        setElementData(root, "dispatch:table", dispatchTable)
        refreshClientGUI()
    end
end
 
 
local dispatchBlips = {}
local allowedFactions = {
    [1] = true,
    [59] = true
}
 
function isInAllowedFactions(element)
    for k,v in pairs(allowedFactions) do
        if exports.factions:isPlayerInFaction(element, k) then
            return true
        end
    end
end
 
function isSameFaction(thePlayer)
    if getElementType(thePlayer) == "player" then
        for key,value in pairs(getElementData(thePlayer, "faction")) do
            for k,v in pairs(getElementData(getLocalPlayer(), "faction")) do
                if key == k then
                    return true
                end
            end
        end
        return getElementData(root, "dispatch:joint")
    else
        return getElementData(thePlayer, "faction") == getElementData(getLocalPlayer(), "faction") or getElementData(root, "dispatch:joint")
    end
end
 
function getCallSign(element)
    if getElementType(element) == "player" then
        callSign = getElementData(element, "dispatch:callsign") or ""
        if callSign == "" then
            for a, b in ipairs(split(getPlayerName(element):gsub("_"," "), ' ')) do
                if tonumber(a) == 1 then
                    callSign = callSign .. b:sub( 1, 1) .. ". "
                else
                    callSign = callSign .. b:sub( 1, #b)
                end
            end
        end
    elseif getElementType(element) == "vehicle" then
        callSign = ""
       
Posted

The script is telling you that processedName is nil. You're trying to assign/call a table's key as a nil value. First try to see if processedName isn't nil, if it's not, then get the type. Something like this would do;

replace;

if type(dispatchTable[processedName]) == "table" then 

with;

if processedName and type(dispatchTable[processedName]) == "table" then 

logo-small.png?v=3 tiny-sapdfr.png

 

If you want to contact me directly concerning Advanced-Gaming, please contact me at [email protected]

Posted
It did help BUT now another error came:

c_dispatch.lue:131: attempt to index global 'dispatchTable' (a nil value)

That's the same error, just about this piece;

if not dispatchTable[processedName] then 
                    dispatchTable[processedName] = { } 
                end 

Add a new check in there to see if you actually got a processedName before trying to create a table for it. Also, make sure that you're actually putting something in the processedName because it might give you unexpected results.

logo-small.png?v=3 tiny-sapdfr.png

 

If you want to contact me directly concerning Advanced-Gaming, please contact me at [email protected]

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