• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

Vote Poll System

God Mythera

Veteran OT User
Joined
Aug 11, 2012
Messages
2,050
Solutions
2
Reaction score
258
Location
United States
Credit goes to: Mock
Original Post: https://otland.net/threads/advanced-poll-system-whit-logs.95258/

Just incase someone wants this for TFS 1.2!

<!-- Gods -->
<talkaction words="/vote" separator=" " script="vote.lua" />

<!-- player talkactions -->
<talkaction words="!yes" script="vote.lua"/>
<talkaction words="!no" script="vote.lua"/>

data/talkactions/scripts/vote.lua
Code:
function vote_clean()
setGlobalStorageValue(9955, 0)
setGlobalStorageValue(2222, 0)
setGlobalStorageValue(2223, 0)
return true
end

function vote_end()
broadcastMessage("Votes on YES - ("..getGlobalStorageValue(2222).."), votes on NO - ("..getGlobalStorageValue(2223)..").")
addEvent(vote_clean, 2000)
return true
end

function onSay(cid, words, param)
local vote_end_time = 60
if getGlobalStorageValue(9955) ~= 1 then
if words == '/vote' then
addEvent(vote_end, vote_end_time * 5000)
broadcastMessage("The question is: "..param..". To vote for YES please use the command \'!yes\', else use the command \'!no\'.")
setGlobalStorageValue(9955, 1)
end
end
if getGlobalStorageValue(9955) == 1 then
if words == '!yes' and getPlayerStorageValue(cid,7236) <= os.time() then
setGlobalStorageValue(2222, getGlobalStorageValue(2222) + 1)
doPlayerSendTextMessage(cid, 19, 'You gave vote in this poll, your voice is YES.')
setPlayerStorageValue(cid,7236, os.time()+(vote_end_time))
elseif words == '!no' and getPlayerStorageValue(cid,7236) <= os.time() then
setGlobalStorageValue(2223, getGlobalStorageValue(2223) + 1)
doPlayerSendTextMessage(cid, 19, 'You gave vote in this poll, your voice is NO.')
setPlayerStorageValue(cid,7236, os.time()+(vote_end_time))
elseif getPlayerStorageValue(cid,7236) >= os.time() then
doPlayerSendTextMessage(cid, 19, "You have already voted in this poll!")
end
else
doPlayerSendTextMessage(cid, 19, "The vote hasn't been started.")
end
return true
end
 
i would suggest making it generic so when you create a poll you can select any options for answer you wish, also i would suggest saving if the player has voted in another way than player storage, since they can just relog
id probably save ip / account id in a global table which gets cleaned together with the voted table
 
i would suggest making it generic so when you create a poll you can select any options for answer you wish, also i would suggest saving if the player has voted in another way than player storage, since they can just relog
id probably save ip / account id in a global table which gets cleaned together with the voted table
I will work on this and update main post soon.
 
i would suggest making it generic so when you create a poll you can select any options for answer you wish, also i would suggest saving if the player has voted in another way than player storage, since they can just relog
id probably save ip / account id in a global table which gets cleaned together with the voted table
id suggest also changing to 1.2 functions as player:~~ then :p
For 1.2
Edited: so it doesn't rely on player storages.
Code:
local t = {
    storage = {
        ['yes'] = 2222,
        ['no'] = 2223,
        ['active'] = 2224
    },
    xMinutes = 1, -- just change this to the number of minutes
    -- leave this as is
    vote_end_time =  xMinutes * (60 * 1000),
    -- each index is a command topic title
    topic = {
        ['fish'] = "Do you like crab cakes",
        ['equipment'] = "Is the equipment on this server overpowering!",
        ['offline training'] = "Do we really need offline training?",
    },
    accountType = ACCOUNT_TYPE_GOD,
    ips = {} -- this is to prevent the same ip voting on the same poll
}

function vote_clean()
    for _, storage in pairs(t.storage) do
        Game.setStorageValue(storage, 0)
    end
  
    for ip in pairs(t.ips) do
        t.ips[ip] = nil
    end
end

function vote_end()
    local Yes, No = Game.getStorageValue(t.storage['yes']), Game.getStorageValue(t.storage['no'])
    broadcastMessage("Votes on YES - ("..Yes.."), votes on NO - ("..No..").")
    addEvent(vote_clean, 2000)
end

function YesOrNo(storage)
    local n = Game.getStorageValue(storage)
    Game.setStorageValue(storage, n > 0 and n + 1 or 1)
end

function onSay(player, words, param)
    if player:getAccountType() >= accountType and Game.getStorageValue(t.storage['active']) < 1 then
        if t.topic[param] then
            addEvent(vote_end, t.vote_end_time)
            broadcastMessage("The question is: "..(t.topic[param]), MESSAGE_STATUS_WARNING)
            broadcastMessage("To vote for YES please use the command '!yes', else use the command '!no'.", MESSAGE_STATUS_WARNING)
            broadcastMessage("Voting will end in " .. t.xMinutes .. " minute" .. (t.xMinutes > 1 and "s!" or "!"), MESSAGE_STATUS_WARNING)
            Game.setStorageValue(t.storage['active'], 1)
        else
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, param..' is not a valid poll.')
        end
        return true
    end

    if Game.getStorageValue(t.storage['active']) > 0 then
        if not t.ips[tostring(player:getIp())] then
            local answer = string.sub(words, 2, 4)
            YesOrNo(answer == 'yes' and t.storage['yes'] or t.storage['no'])
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, 'You gave vote in this poll, your voice is '.. answer ..'.')
            t.ips[tostring(player:getIp())] = true
        else
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You or someone on the same ip has already voted in this poll!")
        end
    else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "There are no active polls at this time.")
    end
    return true
end
 
Last edited:
Back
Top