.:HyPeX:. Posted February 16, 2014 Share Posted February 16, 2014 Hello guys, i got a table where i insert upcoming maps, now, if i want to insert a new map ontop of all of them (on place 1) and move all the rest down, how can i do this? Better explained: MapQueue Table: #1 - Map One #2 - Map Two #3 - Map Three After command: #1 - New Map #2 - Map One #3 - Map Two #4 - Map Three Thanks in advance, HyPeX Link to comment
.:HyPeX:. Posted February 16, 2014 Author Share Posted February 16, 2014 Just a quick update, i found out i can specify a pos at the table and it will automatically move other values down, and now i wana know wich is the minimum value in the table, 0 or 1? table.insert (table, [pos,] value) Link to comment
Moderators Citizen Posted February 16, 2014 Moderators Share Posted February 16, 2014 Everything in Lua are 1 based (and not 0 based like C for example) Link to comment
.:HyPeX:. Posted February 16, 2014 Author Share Posted February 16, 2014 Everything in Lua are 1 based (and not 0 based like C for example) Im facing a problem, how i can find the map name with the event onMapStarting from race gamemode? https://wiki.multitheftauto.com/wiki/Resource:Race I tried mapInfo.name, but it didnt worked. Link to comment
Moderators Citizen Posted February 16, 2014 Moderators Share Posted February 16, 2014 mapInfo.name should work, show me how you did it. Link to comment
.:HyPeX:. Posted February 16, 2014 Author Share Posted February 16, 2014 VotedTable = {} function OnMapStart(mapInfo, mapOptions, gameOptions) RedoVotemapName = mapInfo.name or "Unknown" author = mapInfo.author or "Unknown" voteCount = 0 for v,i in ipairs(getElementsByType("player")) do VotedTable[v] = nil end PlayerCount = getPlayerCount() NeededCountPre = #PlayerCount / 100 NeededCount = NeededCountPre * 60 if ClearNext then Restarted = nil ClearNext = nil end if not Restarted then outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffNow enabled!", getRootElement(), 255,255,255,true) else ClearNext = true outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffRedo Already done, #00cc55disabling votes!", getRootElement(), 255,255,255,true) end end addEventHandler("onMapStarting", getRootElement(), OnMapStart()) function VoteRedo(player,cmd) if not VotedTable[player] then VotedTable[player] = player voteCount = #VotedTable outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaff".. getPlayerNametagText(player).." Voted for Redo!, #00cc55Vote Count: #66ffaa[ ".. voteCount.." / "..NeededCount.." ]", getRootElement(), 255,255,255,true) if voteCount >= NeededCount then RedoVoteMap(RedoVotemapName) end else outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffYou Already #66ffaaVoted!", player, 255,255,255,true) end end addCommandHandler("vr", VoteRedo, false,false) function RedoVoteMap(mapName) mapName = tostring(mapName) if #mapQueue == 0 then outputChatBox("#00aaff[Panel]: Next Map set for Redo", getRootElement(), 255,255,255,true) else outputChatBox("#00aaff[Panel]: Maps Queued for player after Redo", getRootElement(), 255,255,255,true) end table.insert(mapQueue, 1, mapName) end Link to comment
Moderators Citizen Posted February 16, 2014 Moderators Share Posted February 16, 2014 Did you get any error ? Try this: VotedTable = {} function OnMapStart(mapInfo, mapOptions, gameOptions) outputChatBox("Starting map: "..tostring(mapInfo.name)) RedoVotemapName = mapInfo.name or "Unknown" author = mapInfo.author or "Unknown" voteCount = 0 for v,i in ipairs(getElementsByType("player")) do VotedTable[v] = nil end NeededCountPre = getPlayerCount() / 100 NeededCount = NeededCountPre * 60 if ClearNext then Restarted = nil ClearNext = nil end if not Restarted then outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffNow enabled!", getRootElement(), 255,255,255,true) else ClearNext = true outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffRedo Already done, #00cc55disabling votes!", getRootElement(), 255,255,255,true) end end addEventHandler("onMapStarting", getRootElement(), OnMapStart()) function VoteRedo(player,cmd) if not VotedTable[player] then VotedTable[player] = player voteCount = #VotedTable outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaff".. getPlayerNametagText(player).." Voted for Redo!, #00cc55Vote Count: #66ffaa[ ".. voteCount.." / "..NeededCount.." ]", getRootElement(), 255,255,255,true) if voteCount >= NeededCount then RedoVoteMap(RedoVotemapName) end else outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffYou Already #66ffaaVoted!", player, 255,255,255,true) end end addCommandHandler("vr", VoteRedo, false,false) function RedoVoteMap(mapName) mapName = tostring(mapName) if #mapQueue == 0 then outputChatBox("#00aaff[Panel]: Next Map set for Redo", getRootElement(), 255,255,255,true) else outputChatBox("#00aaff[Panel]: Maps Queued for player after Redo", getRootElement(), 255,255,255,true) end table.insert(mapQueue, 1, mapName) end Link to comment
.:HyPeX:. Posted February 16, 2014 Author Share Posted February 16, 2014 No errors, aparently it worked only from client side and sending a trigger to server side, now im facing the trouble of making the table work, it is not counting the votes. (You cant vote again) PS: your code doesnt do anything at all. VotedTable = {} function OnMapStart(MapName) RedoVotemapName = MapName voteCount = 0 for v,i in ipairs(getElementsByType("player")) do VotedTable[i] = nil end local PlayerCount = getPlayerCount() local NeededCountPre = PlayerCount / 100 local NeededCount = NeededCountPre * 60 if ClearNext then Restarted = nil ClearNext = nil end if not Restarted then outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffNow enabled!", getRootElement(), 255,255,255,true) else ClearNext = true outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffRedo Already done, #00cc55disabling votes!", getRootElement(), 255,255,255,true) end end addEvent("RedoVoteInfo",true) addEventHandler("RedoVoteInfo", getRootElement(), OnMapStart) function VoteRedo(player,cmd) if not VotedTable[player] then VotedTable[player] = player local voteCount = #VotedTable local PlayerCount = getPlayerCount() local NeededCountPre = PlayerCount / 100 local NeededCount = NeededCountPre * 60 outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaff".. getPlayerNametagText(player).." Voted for Redo!, #00cc55Vote Count: #66ffaa[ ".. voteCount.." / "..NeededCount.." ]", getRootElement(), 255,255,255,true) if voteCount >= NeededCount then RedoVoteMap(RedoVotemapName) end else outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffYou Already #66ffaaVoted!", player, 255,255,255,true) end end addCommandHandler("vr", VoteRedo, false,false) function RedoVoteMap(mapName) mapName = tostring(mapName) if #mapQueue == 0 then outputChatBox("#00aaff[Panel]: Next Map set for Redo", getRootElement(), 255,255,255,true) else outputChatBox("#00aaff[Panel]: Maps Queued for player after Redo", getRootElement(), 255,255,255,true) end table.insert(mapQueue, 1, mapName) end Link to comment
.:HyPeX:. Posted February 16, 2014 Author Share Posted February 16, 2014 Bump, i did this work perfectly, with a single exception, i cant seem to be able to count the table, wich is driving me crazy as fk. Im blocked to vote again, it says you already voted, but #Table displays 0. wth is going on here? VotedTable = {} function OnMapStart(MapName) RedoVotemapName = MapName for v,i in ipairs(getElementsByType("player")) do VotedTable[v] = nil end local PlayerCount = getPlayerCount() local NeededCountPre = PlayerCount / 100 local NeededCount = NeededCountPre * 60 if ClearNext then Restarted = nil ClearNext = nil end if not Restarted then outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffNow enabled!", getRootElement(), 255,255,255,true) else ClearNext = true outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffRedo Already done, #00cc55disabling votes!", getRootElement(), 255,255,255,true) end end function onMapStart2() if AlreadyDone then AlreadyDone = nil end end addEventHandler("onResourceStart", getRootElement(), onMapStart2) function onMapStart1(MapName) if AlreadyDone then return end AlreadyDone = true OnMapStart(MapName) end addEvent("RedoVoteInfo",true) addEventHandler("RedoVoteInfo", getRootElement(), onMapStart1) function VoteRedo(player,cmd) if not VotedTable[player] then VotedTable[player] = player local voteCount = #VotedTable local PlayerCount = getPlayerCount() local NeededCountPre = PlayerCount / 100 local NeededCount = NeededCountPre * 60 outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaff".. getPlayerNametagText(player).." Voted for Redo!, #00cc55Vote Count: #66ffaa[ ".. voteCount.." / "..NeededCount.." ]", getRootElement(), 255,255,255,true) if voteCount >= NeededCount then RedoVoteMap(RedoVotemapName) end else outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffYou Already #66ffaaVoted!", player, 255,255,255,true) end end addCommandHandler("vr", VoteRedo, false,false) function RedoVoteMap(mapName) mapName = tostring(mapName) if #mapQueue == 0 then outputChatBox("#00aaff[Panel]: Next Map set for Redo", getRootElement(), 255,255,255,true) else outputChatBox("#00aaff[Panel]: Maps Queued for player after Redo", getRootElement(), 255,255,255,true) end table.insert(mapQueue, 1, mapName) end Link to comment
Moderators Citizen Posted February 16, 2014 Moderators Share Posted February 16, 2014 No errors, aparently it worked only from client side and sending a trigger to server side, now im facing the trouble of making the table work, it is not counting the votes. (You cant vote again)PS: your code doesnt do anything at all. First, it must be a server side script (you were talking about client side so I'm not sure). Then, my code was supposed to write: Starting map: in the chatbox, didn't it ? For the vote, it's maybe because you should reset it properly. Try to replace this: for v,i in ipairs(getElementsByType("player")) do VotedTable[v] = nil end by this: VotedTable = {} Not sure if it was the problem though. _________________________ (oh you wrote another post) Im blocked to vote again, it says you already voted, but #Table displays 0. When you are "inserting" rows into a table using specific key (here the player element) the # will always return 0 (except if you use table.insert or using numbers as key) The only way to get the real length is by doing a function like this: function tablelength(t) local count = 0 for _ in pairs(t) do count = count + 1 end return count end so: voteCount = #VotedTable will become: voteCount = tablelength(VotedTable) Hope it will fix ur script. Link to comment
.:HyPeX:. Posted February 16, 2014 Author Share Posted February 16, 2014 The client side Just outputs correctly the name: function handleMapInfo(mapInfo) name = mapInfo.name or "Unknown" triggerServerEvent("RedoVoteInfo",getRootElement(),name) outputChatBox("Map: ".. name, 0,175,255,true) end addEventHandler("onClientMapStarting",getRootElement(), handleMapInfo) But, then, when it is set by voteredo, map name is unknow.. idk why: Server side: VotedTable = {} VotedTable2= {} function OnMapStart(mapName) RedoSet = nil VotedTable = {} VotedTable2 = {} if ClearNext then Restarted = nil ClearNext = nil end if not Restarted then outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffNow enabled!", getRootElement(), 255,255,255,true) else ClearNext = true outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffRedo Already done, #00cc55disabling votes!", getRootElement(), 255,255,255,true) end end function onMapStart2() if NextRedoSet then NextRedoSet = nil end if AlreadyDone then AlreadyDone = nil end end addEventHandler("onResourceStart", getRootElement(), onMapStart2) function onMapStart1(MapName) if AlreadyDone then return end AlreadyDone = true OnMapStart() RedoMap = MapName end addEvent("RedoVoteInfo",true) addEventHandler("RedoVoteInfo", getRootElement(), onMapStart1) function tablelength(t) local count = 0 for _ in pairs(t) do count = count + 1 end return count end function VoteRedo(player,cmd) if not VotedTable2[player] then VotedTable2[player] = player table.insert(VotedTable, player) local voteCount = tablelength(VotedTable) local PlayerCount = getPlayerCount() local NeededCountPre = PlayerCount / 100 local NeededCount = NeededCountPre * 60 outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaff".. getPlayerNametagText(player).." Voted for Redo!, #00cc55Vote Count: #66ffaa[ ".. voteCount.." / "..NeededCount.." ]", getRootElement(), 255,255,255,true) if voteCount >= NeededCount then local mapName = RedoVotemapName if not NextRedoSet then NextRedoSet = true local redoMap = RedoMap AddMap2("apanel", redoMap, true) end end else outputChatBox("#00aaff[Vote#00cc55Redo]: #00aaffYou Already #66ffaaVoted!", player, 255,255,255,true) end end addCommandHandler("vr", VoteRedo, false,false) function RedoVoteMap(map) if RedoSet then return end if #mapQueue == 0 then outputChatBox("#00aaff[Panel]: Next Map set for Redo", getRootElement(), 255,255,255,true) else triggerEvent("onBoughtMap",getRootElement(),mapQueue[1]) outputChatBox("#00aaff[Panel]: Maps Queued for player after Redo", getRootElement(), 255,255,255,true) end RedoSet = true table.insert(mapQueue, 1, mapName) callClientFunction(getRootElement(),"updateMapQueueList",mapQueue) end -- Buy a next map function AddMap2(thePlayer,mapName,command) local account = getAccount("apanel") if not (isGuestAccount(account)) then local playerCash = tonumber(getAccountData(account,"cash")) if not (mapName == "") then if playerCash >= 0 then if command then mapName = getMapName(mapName) else mapName = tostring(mapName) end table.insert(mapQueue,1,mapName) local freeMaps = tonumber(getAccountData(account,"freeMaps")) if freeMaps ~= 0 then addStat(account,"freeMaps",-1) else setAccountData(account,"cash",playerCash - 0) end addStat(account,"buyedMaps",1) setTimer(resetMapTimer,900000,1,mapName) outputChatBox("#00aaff[Panel]: Map ".. mapName .." was set for redo!", getRootElement(), 255,255,255,true) callClientFunction(getRootElement(),"updateMapQueueList",mapQueue) else showServerMsg(thePlayer,"Add Queue","#FFFFFFYou don't have enough money to set a map!") end else showServerMsg(thePlayer,"Add Queue","#FFFFFFPlease select a map from the list first!") end end end Also, the 60% of players gives most times a decimal number, how can i round it up or down? Link to comment
Mega9 Posted February 17, 2014 Share Posted February 17, 2014 Used to round decimal numbers math.ceil Link to comment
ixjf Posted February 17, 2014 Share Posted February 17, 2014 Just a quick update, i found out i can specify a pos at the table and it will automatically move other values down, and now i wana know wich is the minimum value in the table, 0 or 1? table.insert (table, [pos,] value) There is no real minimum index you can specify for a table, but table functions will only count from 1 onwards (table.insert, ipairs, ...). Link to comment
.:HyPeX:. Posted February 17, 2014 Author Share Posted February 17, 2014 Found out in the end that the script was not compatible with adding an object ontop, so i simply now add it on the last place and it works perfectly 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