Blinker. Posted January 15, 2015 Share Posted January 15, 2015 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
novo Posted January 16, 2015 Share Posted January 16, 2015 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
Blinker. Posted January 16, 2015 Author Share Posted January 16, 2015 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 .. Link to comment
Mizudori Posted January 16, 2015 Share Posted January 16, 2015 http://www.lua.org/pil/19.2.html Link to comment
Blinker. Posted January 17, 2015 Author Share Posted January 17, 2015 I know insert and remove , but this method should work too .. but i don't know the problem Link to comment
Anubhav Posted January 17, 2015 Share Posted January 17, 2015 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
MTA Team botder Posted January 17, 2015 MTA Team Share Posted January 17, 2015 Maps[i] = getResourceInfo(map, "name") Change it to: Maps[#Maps + 1] = getResourceInfo(map, "name") Link to comment
Blinker. Posted January 17, 2015 Author Share Posted January 17, 2015 @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
Anubhav Posted January 17, 2015 Share Posted January 17, 2015 Wrong syntax. Line 17 should be: outputChatBox('1') Link to comment
Blinker. Posted January 17, 2015 Author Share Posted January 17, 2015 nah , its same you can write it like that outputChatBox'1' or outputChatBox("1") Link to comment
Et-win Posted January 17, 2015 Share Posted January 17, 2015 ...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. Link to comment
Blinker. Posted January 18, 2015 Author Share Posted January 18, 2015 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
Et-win Posted January 18, 2015 Share Posted January 18, 2015 It does work in my server. Link to comment
MTA Team botder Posted January 18, 2015 MTA Team Share Posted January 18, 2015 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
Blinker. Posted January 18, 2015 Author Share Posted January 18, 2015 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. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now