Jump to content

Weird Problem


Blinker.

Recommended Posts

Posted

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.

Posted

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.

Posted

Hello novo ,

my problem isn't with many words .. , i mean i only need 1 word like "/check ZY" and the script should output "[DD] Cross ZY"

but it's not working ..

Posted
  
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) 
  

  • MTA Team
Posted
Maps[i] = getResourceInfo(map, "name") 

Change it to:

Maps[#Maps + 1] = getResourceInfo(map, "name") 

Posted

@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"

Posted

...You could've found the problem already at the time it didn't work. Why not putting 'outputChatBox("Test")' in every function, if, for, etc.? It will show you exactly where the script fails/stops.

Posted

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 
  
  
  

  • MTA Team
Posted
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" 
} 

Posted

Necktrox thanks for the tip ...

Et-win it was working in my server , but now it stopped working .. no idea what's the problem

EDIT : ah i tested it on my friend's server and its working well .. thank you all for help.

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