• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[LUA Request] Modal Window changemap with votes

Rebine

New Member
Joined
Feb 10, 2010
Messages
69
Reaction score
2
Location
Chile
Hello everyone, I am creating my war server but I need this script. . .

* Modal Window changemap with votes.

In my server there are different types of maps, each with their respective modalities, Deathmach, CTF and King of the Game, I would like the script to vote 3 maps, one of each type, taken at random.

First of all, Thanks
I wait for comments :) and I apologize for my English

TFS 1.2 - 8.60
 
Last edited:
In talkactions.xml
XML:
<talkaction words="!vote" script="vote.lua" />
in vote.lua
Lua:
votemapconfig = {
storage = 2000, -- player storage value, to store votes
globalstorage = 2000, -- global storage value, for map change cooldown
timer1 = 10, -- seconds to teleport everybody
timer2 = 1, -- minutes per gamemode
percent = 50 -- % of active players to vote to cause map change
}

function onSay(player, words, param)
maps = {
   [1] = {pos = Position(1010,1004,6), name = "Map1"},
   [2] = {pos = Position(1012,1004,6), name = "Map2"},
   [3] = {pos = Position(1014,1004,6), name = "Map3"}
   }
    local currenttime = tonumber(os.date("%H%M", os.time()))
    local stor = Game.getStorageValue(votemapconfig.globalstorage) or 0
    local h,m =  math.floor(stor/100), stor-math.floor(stor/100)*100
    if stor > 0 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Game mode was changed at "..h..":"..m..", cooldown is "..votemapconfig.timer2.." minutes, please wait "..currenttime-stor.." minutes before attempting to change the map again.")
        return false
    end
     local padWindow = ModalWindow(2000, "Select map to vote for:", "Total Players:"..Game.getPlayerCount().."")
        for i=1,3 do
            padWindow:addChoice(i, maps[i].name .. ", Votes: "..votes[i]..".")
        end
     padWindow:addButton(1, "VOTE")
     padWindow:addButton(2, "EXIT")
     padWindow:setDefaultEnterButton(1)
     padWindow:setDefaultEscapeButton(2)
     padWindow:sendToPlayer(player)
   return false
end
In creaturescripts.xml
XML:
<event type="modalwindow" name="votemap" script="votemap.lua"/>
   <event type="login" name="votemaplogin" script="votemap.lua"/>
in votemap.lua
Lua:
votes = {0,0,0}
voters = {}
function winner(Table) -- Couldn't find an easier way to do this lol
    local high = 1
    for i, k in ipairs(Table) do
        if Table[i] > high then
            high = Table[i]
        end
    end
    return high
end
function onModalWindow(player, modalWindowId, buttonId, choiceId)
    local currenttime = tonumber(os.date("%H%M", os.time()))
    local players = Game.getPlayerCount()
    if modalWindowId == 2000 then
        if buttonId == 1 then
            if player:getStorageValue(votemapconfig.storage) > 0 then
                player:sendTextMessage(MESSAGE_INFO_DESCR, "You have already voted for "..maps[player:getStorageValue(votemapconfig.storage)].name..".  You must wait until the next map change to vote again.")
                return false
            else
                player:sendTextMessage(MESSAGE_INFO_DESCR, "You have voted for "..maps[choiceId].name..", when half or more of the active players have voted, the game mode will change.")
                player:setStorageValue(votemapconfig.storage,choiceId)
                votes[choiceId] = votes[choiceId]+1
                voters[#voters+1] = player
                if #voters > players*(votemapconfig.percent/100) then
                    Game.setStorageValue(votemapconfig.globalstorage,currenttime)
                        for i=1,#voters do
                            voters[i]:setStorageValue(votemapconfig.storage,-1)
                        end
                    votes = {0,0,0}
                    voters = {}
                    Game.broadcastMessage("Enough players have voted! The next map is "..maps[choiceId].name.."!", MESSAGE_EVENT_DEFAULT)
                    Game.broadcastMessage("Teleporting in "..votemapconfig.timer1.." seconds.", MESSAGE_INFO_DESCR)
                    addEvent(function()
                        for _, targetPlayer in ipairs(Game.getPlayers()) do
                            targetPlayer:teleportTo(maps[winner(votes)].pos)
                            maps[winner(votes)].pos:sendMagicEffect(CONST_ME_TELEPORT)
                        end
                        addEvent(function()
                            Game.setStorageValue(votemapconfig.globalstorage,-1)  
                        end,votemapconfig.timer2*60*1000)
                    end,votemapconfig.timer1*1000)
                end
            end
        end
    end
    return true
end

function onLogin(player)
   player:registerEvent("votemap")
   return true
end
Tested on TFS 1.2 (with one player :()
let me know if you run into any problems with it
 
Last edited:
Back
Top