Jump to content

Weird Problem


Blinker.

Recommended Posts

Hello ,

  
function maps() 
Maps = {} 
for i, map in ipairs (exports.mapmanager:getMapsCompatibleWithGamemode(exports.mapmanager:getRunningGamemode() )) do 
    if get(getResourceName(map)..".#respawn") == "none" then 
    Maps[i] = getResourceInfo(map, "name") 
    end 
end 
end 
  
  
function check(p,_,mapName) 
maps() 
for i, v in ipairs (Maps) do 
outputChatBox(i.." - "..v) 
if string.find(v,string.upper(mapName)) then 
outputChatBox("map found") 
end 
end 
end 
addCommandHandler("check",check) 
  
  
  
  

no errors , the script doesn't output anything.

any help would be greatly appreciated.

Link to comment

I see you're using a single variable for retrieving the inputted map name, which means only the first introduced argument when running the command will be held.

(arg1, arg2, ...: Each word after command name in the original command is passed here in a seperate variable. If there is no value for an argument, its variable will contain nil. You can deal with a variable number of arguments using the vararg expression, as shown in Server Example 2 below.)

In order to avoid this and to retrieve all the inputted arguments you could do the following:

  
function maps () 
    local mapsList = {} 
    for i, map in ipairs (exports.mapmanager:getMapsCompatibleWithGamemode(exports.mapmanager:getRunningGamemode())) do 
        if get(getResourceName(map)..".#respawn") == "none" then 
            mapsList[i] = getResourceInfo(map, "name") 
        end 
    end 
    return mapsList -- As we're calling the function each time the command is executed, lets return the generated table instead of establishing a global variable 
end 
function check (player, _, ...) -- "..." refers to all the following arguments 
    local arguments = {...} -- By doing this we insert all those arguments we didn't hold onto any variable into a table 
    if #arguments == 0 then return end -- Stop the function's execution in case no arguments were introduced 
    local mapName = table.concat(arguments, " ") -- This will convert the above table into a string, which will be formed by any value the table contains itself separated by spaces 
    local maps = maps() -- mapsList 
    for i,v in ipairs(maps) do -- ipairs(maps()) if we want to avoid creating an unnecessary extra variable 
        if string.find(v, string.lower(mapName) then -- mapName:lower() = string.lower(mapName) 
            outputChatBox("Map found: "..v) 
        end 
    end 
end 
addCommandHandler("check", check) 
  

And well, make sure that "mapsList" contains valid maps as I'm not sure that ".#respawn" is the setting name normally used.

Link to comment
  
function maps() 
    Maps = {} 
    for i, map in ipairs (exports.mapmanager:getMapsCompatibleWithGamemode(exports.mapmanager:getRunningGamemode() )) do 
        if get(getResourceName(map)..".#respawn") == "none" then 
            Maps[i] = getResourceInfo(map, "name") 
        end 
    end 
    return Maps 
end 
  
  
function check(p,_,mapName) 
 local map = maps() 
    for i, v in ipairs (map) do 
        outputChatBox(i.." - "..v) 
        if string.find(v, string.upper(mapName)) then 
            outputChatBox("map found") 
        end 
    end 
end 
addCommandHandler("check",check) 
  

Link to comment

@Anubhav , Necktrox it doesn't work..

well i tried that

  
Maps = {} 
function maps() 
for i, map in ipairs (exports.mapmanager:getMapsCompatibleWithGamemode(exports.mapmanager:getRunningGamemode() )) do 
  
    if get(getResourceName(map)..".#respawn") == "none" then 
    Maps[i] = getResourceInfo(map, "name") 
    outputChatBox(Maps[i]) 
    end 
end 
end 
  
  
maps() 
  
for i ,v in ipairs (Maps) do 
outputChatBox'1' 
  
  
end 
  
  
  
  
  

it will output the maps but it won't output "1"

Link to comment

i did that

  
  
  
Maps = {} 
function maps() 
for i, map in ipairs (exports.mapmanager:getMapsCompatibleWithGamemode(exports.mapmanager:getRunningGamemode() )) do 
  
    if get(getResourceName(map)..".#respawn") == "none" then 
    Maps[i] = getResourceInfo(map, "name") 
    outputChatBox(Maps[i]) -- it does output the maps , so no problem here 
    end 
end 
end 
  
  
maps() 
  
for i ,v in ipairs (Maps) do 
outputChatBox'1' -- doesn't output 1 so it fails here 
  
  
end 
  
  
  

Link to comment
  • MTA Team
for i, map in ipairs(...) do 
    -- ... 
    Maps[i] = getResourceInfo(map, "name") 
end 

I wanted to tell you that this code may leave empty nil slots in the table Maps, which will make your loop later fail.

That is how your table may look like (= No map found)

Maps = { 
    [1] = nil, 
    [2] = "Map Name A", 
    [3] = nil, 
    [4] = nil, 
    [5] = nil, 
    [6] = "Map Name B", 
    [7] = "Map Name A" 
} 

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