Jump to content

What wrong in this vote script?


Turbe$Z

Recommended Posts

57 minutes ago, Tails said:

Why not use ... to get the args and play around with them? Here:


addCommandHandler("k",
    function(_,...)
        if #arg < 3 then
            outputChatBox("Not enough arguments supplied")
        else
            local q = table.concat(arg," ", 1, #arg-2)
            local a1 = arg[#arg-1]
            local a2 = arg[#arg]
            
            outputChatBox("Question: "..q)
            outputChatBox("Answer 1: "..a1)
            outputChatBox("Answer 2: "..a2)
        end
    end
,false,false)

It does exactly what you're asking. Example: /k Is this a question? yes no

This way only 1-word-answers are possible.

14 hours ago, LoPollo said:

v2 will have the same value as valasz, i don't think you want this. What this event should work with? Think what the event need to know from the function that trigger it

Shouldn't be the 2 parameters the 2 answers? I'm not sure... i lost myself in all these replies xD

Can @Turbo777 re-post the whole edited code?

Link to comment
2 minutes ago, LoPollo said:

This way only 1-word-answers are possible.

Can @Turbo777 re-post the whole edited code?

local szavon = false
local ido = 30
local valasz = 0
local valasz2 = 0
    GUIEditor_Window = {}
    GUIEditor_Label = {}
function szAblak(ker,v1,v2)
    if not szavon then
        local v_1 = v1
        local v_2 = v2
        local szavon = true

        local sw,sh = guiGetScreenSize() 

        GUIEditor_Window[1] = guiCreateWindow(sw/1.48,sh/1.4,sw/3.2,sh/3.6,"Szavazás",false)
        guiWindowSetMovable(GUIEditor_Window[1],false)
        guiWindowSetSizable(GUIEditor_Window[1],false)
        GUIEditor_Label[1] = guiCreateLabel(0.0474,0.1242,0.9161,0.2919,ker,true,GUIEditor_Window[1])
        guiLabelSetHorizontalAlign(GUIEditor_Label[1],"center",true)
        guiSetFont(GUIEditor_Label[1],"default-bold-small")
        GUIEditor_Label[2] = guiCreateLabel(0.0657,0.4783,0.8759,0.1553,"1.) "..v_1,true,GUIEditor_Window[1])
        --guiLabelSetColor(GUIEditor_Label[2],255,200,200)
        --guiSetFont(GUIEditor_Label[2],"default-bold-small")
        GUIEditor_Label[3] = guiCreateLabel(0.0657,0.6335,0.8759,0.1491,"2.) "..v_2,true,GUIEditor_Window[1])
        GUIEditor_Label[4] = guiCreateLabel(0.0474,0.8075,0.8,0.1,ido,true,GUIEditor_Window[1])
        guiLabelSetColor(GUIEditor_Label[4],150,150,255)
        guiSetFont(GUIEditor_Label[4],"clear-normal")
        bindKey ("1", "down", 
            function()
                guiSetFont(GUIEditor_Label[3],"clear-normal")
                guiLabelSetColor(GUIEditor_Label[3],255,255,255)
                valasz = 1
                guiLabelSetColor(GUIEditor_Label[2],255,200,200)
                guiSetFont(GUIEditor_Label[2],"default-bold-small")
            end) 
        bindKey ("2", "down", 
            function()
                guiSetFont(GUIEditor_Label[2],"default-normal")
                guiLabelSetColor(GUIEditor_Label[2],255,255,255)
                valasz2 = 2
                guiLabelSetColor(GUIEditor_Label[3],255,200,200)
                guiSetFont(GUIEditor_Label[3],"default-bold-small")
            end)

        lejar = setTimer(
            function()
                ido = ido-1
                guiSetText(GUIEditor_Label[4], ido )

                if ido == 0 then
                    szVeg()
                    --killTimer(lejar)
                end
            end, 1000,30)
    end
end

function szVeg()
    if isTimer(lejar) then
        killTimer(lejar)
    end
    szavon = false
    guiSetText(GUIEditor_Label[4], "szavazatok számlálása..." )
    guiSetFont(GUIEditor_Label[2],"default-normal")
    guiSetFont(GUIEditor_Label[3],"default-normal")
                guiLabelSetColor(GUIEditor_Label[3],255,255,255)
                guiLabelSetColor(GUIEditor_Label[2],255,255,255)
    --("valasz: "..valasz)
	local v2 = valasz2
	triggerServerEvent("submit",getLocalPlayer(),valasz,v2) 


        unbindKey ("1", "down", 
            function()
                guiSetFont(GUIEditor_Label[3],"clear-normal")
                guiLabelSetColor(GUIEditor_Label[3],255,255,255)
                valasz = 1
                guiLabelSetColor(GUIEditor_Label[2],255,200,200)
                guiSetFont(GUIEditor_Label[2],"default-bold-small")
            end) 
        unbindKey ("2", "down", 
            function()
                guiSetFont(GUIEditor_Label[2],"default-normal")
                guiLabelSetColor(GUIEditor_Label[2],255,255,255)
                valasz2 = 2
                guiLabelSetColor(GUIEditor_Label[3],255,200,200)
                guiSetFont(GUIEditor_Label[3],"default-bold-small")
            end)


end

addEvent("szvzas",true)
addEventHandler("szvzas",root,
function(question,answ1,answ2) --the handler attached to the root (resourceRoot should also work) accept 3 arguments
	szAblak(question,answ1,answ2) --and call the function with these paramenters
end)

 
addEvent("valaszok",true)
addEventHandler("valaszok",getRootElement(),
function(v1,v2)
	guiSetText(GUIEditor_Label[4], "_@/\"" )
    guiSetText(GUIEditor_Label[2], guiGetText(GUIEditor_Label[2]) .. " - " .. v1 )
    guiSetText(GUIEditor_Label[3], guiGetText(GUIEditor_Label[3]) .. " - " .. v2 )
     if v1 > v2 then
         guiSetFont(GUIEditor_Label[2],"default-bold-small")
     elseif v1 < v2 then
         guiSetFont(GUIEditor_Label[3],"default-bold-small")
     end
    setTimer(
        function()
            destroyElement(GUIEditor_Window[1])
            szavon = false
            ido = 30
            valasz = 0
            valasz2 = 0
        end, 5000, 1)    
 end)

 

Link to comment
12 minutes ago, LoPollo said:

This way only 1-word-answers are possible.

15 hours ago, LoPollo said:
addCommandHandler("k",
    function(_,...)
        if #arg < 3 then
            outputChatBox("Not enough arguments supplied")
        else
            local q,a1,a2 = unpack(split(table.concat(arg," ",2),","))
            
            outputChatBox("Question: "..q)
            outputChatBox("Answer 1: "..a1)
            outputChatBox("Answer 2: "..a2)
        end
    end
,false,false)

Usage: /k Is this a question?, Yes it is, No it's not

Edited by Tails
  • Like 1
Link to comment
57 minutes ago, Tails said:

addCommandHandler("k",
    function(_,...)
        if #arg < 3 then
            outputChatBox("Not enough arguments supplied")
        else
            local q,a1,a2 = unpack(split(table.concat(arg," ",2),","))
            
            outputChatBox("Question: "..q)
            outputChatBox("Answer 1: "..a1)
            outputChatBox("Answer 2: "..a2)
        end
    end
,false,false)

Usage: /k Is this a question?, Yes it is, No it's not

This is what i was thinking about, but comma is too much used in questions. I think that semicolon ; will fit that role better.

Also i don't think that turbo has its script working 100%, i'm now going to read it

 

@Turbo777 post also the serverside, so i can see the event. I will read from the command handler and try to find if every variable is where it's supposed to be.

 

PS: next time you make a script try to make it in english. Give every function a "logic" name, so the code will be much more easier to read since it we can guess what every line should do. Otherwise commenting your code will be helpful

Edited by LoPollo
Link to comment
2 minutes ago, LoPollo said:

This is what i was thinking about, but comma is too much used in questions. I think that semicolon ; will fit that role better.

Also i don't think that turbo has its script working 100%, i'm now going to read it

 

@Turbo777 post also the serverside, so i can see the event. I will read from the command handler and try to find if every variable is where it's supposed to be

oh sorry :D 

--server--

addEvent( "submit", true )
addEventHandler( "submit", root,
function (v1, v2)
setTimer(
        function()
			triggerClientEvent(root, "valaszok", resourceRoot, v1, v2)
        end, 5000, 1)  
end
)

function szvzas(playerSource, cmd, theQuestion, theFirstAnswer, theSecondAnswer) --read the wiki for more infos
    triggerClientEvent ( root, "szvzas", resourceRoot, theQuestion, theFirstAnswer, theSecondAnswer ) --trigger on root, event "szvzas", source is the resource, and there are 3 arguments
end
addCommandHandler ( "k", szvzas )

 

Link to comment

Client:

local szavon = false --is this something used to prevent double questions? it should be serverside :/
local ido = 30
local valasz = 0 
local valasz2 = 0
-- I need an explanation of valasz and valasz2, if you use them to keep track of the selected answer then there are errors


GUIEditor_Window = {}
GUIEditor_Label = {}

function szAblak(ker,v1,v2) --question, answ1, answ2, all strings
	if not szavon then 
		local v_1 = v1 --answ1, useless variable
		local v_2 = v2 --answ2, useless variable
		local szavon = true

		local sw,sh = guiGetScreenSize() 

		GUIEditor_Window[1] = guiCreateWindow(sw/1.48,sh/1.4,sw/3.2,sh/3.6,"Szavazás",false) --wow it's hard to me thinking relatives coord like this, wouldn't be % much easier?
		guiWindowSetMovable(GUIEditor_Window[1],false)
		guiWindowSetSizable(GUIEditor_Window[1],false)

		GUIEditor_Label[1] = guiCreateLabel(0.0474,0.1242,0.9161,0.2919,ker,true,GUIEditor_Window[1])
		guiLabelSetHorizontalAlign(GUIEditor_Label[1],"center",true)
		guiSetFont(GUIEditor_Label[1],"default-bold-small")

		GUIEditor_Label[2] = guiCreateLabel(0.0657,0.4783,0.8759,0.1553,"1.) "..v_1,true,GUIEditor_Window[1])

		GUIEditor_Label[3] = guiCreateLabel(0.0657,0.6335,0.8759,0.1491,"2.) "..v_2,true,GUIEditor_Window[1])

		GUIEditor_Label[4] = guiCreateLabel(0.0474,0.8075,0.8,0.1,ido,true,GUIEditor_Window[1])
		guiLabelSetColor(GUIEditor_Label[4],150,150,255)
		guiSetFont(GUIEditor_Label[4],"clear-normal")

		bindKey ("1", "down", 
			function()
				guiSetFont(GUIEditor_Label[3],"clear-normal")
				guiLabelSetColor(GUIEditor_Label[3],255,255,255)
				valasz = 1
				guiLabelSetColor(GUIEditor_Label[2],255,200,200)
				guiSetFont(GUIEditor_Label[2],"default-bold-small")
			end
		) 
		bindKey ("2", "down", 
			function()
				guiSetFont(GUIEditor_Label[2],"default-normal")
				guiLabelSetColor(GUIEditor_Label[2],255,255,255)
				valasz2 = 2
				guiLabelSetColor(GUIEditor_Label[3],255,200,200)
				guiSetFont(GUIEditor_Label[3],"default-bold-small")
			end
		)

		lejar = setTimer(
			function()
				ido = ido-1
				guiSetText(GUIEditor_Label[4], ido )

				if ido == 0 then
					szVeg() --so it will be called 30s after szAblak
				end
			end, 1000,30)
	end
end

function szVeg()
	if isTimer(lejar) then --is it possible? It's useless i think
		killTimer(lejar)
	end
	szavon = false
	guiSetText(GUIEditor_Label[4], "szavazatok számlálása..." )
	guiSetFont(GUIEditor_Label[2],"default-normal")
	guiSetFont(GUIEditor_Label[3],"default-normal")
	guiLabelSetColor(GUIEditor_Label[3],255,255,255)
	guiLabelSetColor(GUIEditor_Label[2],255,255,255)
	local v2 = valasz2 --if 2 was pressed this will be 2, otherwise 0
	triggerServerEvent("submit",getLocalPlayer(),valasz,v2) --valasz will be 1 if 1 key got pressed, otherwise 0


	unbindKey ("1", "down", 
		function()
			guiSetFont(GUIEditor_Label[3],"clear-normal")
			guiLabelSetColor(GUIEditor_Label[3],255,255,255)
			valasz = 1
			guiLabelSetColor(GUIEditor_Label[2],255,200,200)
			guiSetFont(GUIEditor_Label[2],"default-bold-small")
		end
	) 
	unbindKey ("2", "down", 
		function()
			guiSetFont(GUIEditor_Label[2],"default-normal")
			guiLabelSetColor(GUIEditor_Label[2],255,255,255)
			valasz2 = 2
			guiLabelSetColor(GUIEditor_Label[3],255,200,200)
			guiSetFont(GUIEditor_Label[3],"default-bold-small")
		end
	)
end

addEvent("szvzas",true) --this will be called once a one player in the server makes a question
addEventHandler("szvzas",root,
	function(question,answ1,answ2) --the handler attached to the root (resourceRoot should also work) accept 3 arguments
		szAblak(question,answ1,answ2) --and call the function with these paramenters
	end
)

 
addEvent("valaszok",true)
addEventHandler("valaszok",getRootElement(),
function(v1,v2) --after 5 second from szVeg this will be triggered
	--v1 1 or 0, v2 2 or 0. BOTH CAN BE 0!!!! ALSO BOTH CAN BE 1 AND 2. The font wans't already set? you are not changing any passed var serverside. This doesn't make sense
	guiSetText(GUIEditor_Label[4], "_@/\"" )
	guiSetText(GUIEditor_Label[2], guiGetText(GUIEditor_Label[2]) .. " - " .. v1 )
	guiSetText(GUIEditor_Label[3], guiGetText(GUIEditor_Label[3]) .. " - " .. v2 )
	if v1 > v2 then --i need explanation here. It's error if you are using them to see what localPlayer choose
		guiSetFont(GUIEditor_Label[2],"default-bold-small")
	elseif v1 < v2 then
		guiSetFont(GUIEditor_Label[3],"default-bold-small")
	end

	setTimer(
		function()--reset to default
			destroyElement(GUIEditor_Window[1])
			szavon = false
			ido = 30
			valasz = 0
			valasz2 = 0
		end,
	5000, 1)

end
)

Server

addEvent( "submit", true )
addEventHandler( "submit", root,
	function (v1, v2) -- 1 or 0, 2 or 0 depending on keypress
		setTimer(
			function()
				triggerClientEvent(root, "valaszok", resourceRoot, v1, v2)
			end, 
		5000, 1)  
	end
)

function szvzas(playerSource, cmd, theQuestion, theFirstAnswer, theSecondAnswer) --read the wiki for more infos
	triggerClientEvent ( root, "szvzas", resourceRoot, theQuestion, theFirstAnswer, theSecondAnswer ) --trigger on root, event "szvzas", source is the resource, and there are 3 arguments
end
addCommandHandler ( "k", szvzas )

Check the comments, starting from the cmdHandler serverside, following the functions. Like if you were the computer

 

Even if clients check if a "poll" is ongoing, wouldn't be better to make the check serverside? less events triggered means less bandwidth used, more reliable, more secure, much faster. I'm talking about szavon

What are valasz and valasz2 used for? check my comments. I can't understand the part

 

Why is the event submit serverside? it will be spammed: every player will trigger it, and for every trigger it will trigger another event to the whole server!!!

 

let's start with these. PS keep in mind the commandhandler of @Tails because once we get this working we will use it

Edited by LoPollo
Link to comment
20 minutes ago, LoPollo said:

Client:


local szavon = false --is this something used to prevent double questions? it should be serverside :/local ido = 30local valasz = 0 local valasz2 = 0-- I need an explanation of valasz and valasz2, if you use them to keep track of the selected answer then there are errorsGUIEditor_Window = {}GUIEditor_Label = {}function szAblak(ker,v1,v2) --question, answ1, answ2, all strings	if not szavon then 		local v_1 = v1 --answ1, useless variable		local v_2 = v2 --answ2, useless variable		local szavon = true		local sw,sh = guiGetScreenSize() 		GUIEditor_Window[1] = guiCreateWindow(sw/1.48,sh/1.4,sw/3.2,sh/3.6,"Szavazás",false) --wow it's hard to me thinking relatives coord like this, wouldn't be % much easier?		guiWindowSetMovable(GUIEditor_Window[1],false)		guiWindowSetSizable(GUIEditor_Window[1],false)		GUIEditor_Label[1] = guiCreateLabel(0.0474,0.1242,0.9161,0.2919,ker,true,GUIEditor_Window[1])		guiLabelSetHorizontalAlign(GUIEditor_Label[1],"center",true)		guiSetFont(GUIEditor_Label[1],"default-bold-small")		GUIEditor_Label[2] = guiCreateLabel(0.0657,0.4783,0.8759,0.1553,"1.) "..v_1,true,GUIEditor_Window[1])		GUIEditor_Label[3] = guiCreateLabel(0.0657,0.6335,0.8759,0.1491,"2.) "..v_2,true,GUIEditor_Window[1])		GUIEditor_Label[4] = guiCreateLabel(0.0474,0.8075,0.8,0.1,ido,true,GUIEditor_Window[1])		guiLabelSetColor(GUIEditor_Label[4],150,150,255)		guiSetFont(GUIEditor_Label[4],"clear-normal")		bindKey ("1", "down", 			function()				guiSetFont(GUIEditor_Label[3],"clear-normal")				guiLabelSetColor(GUIEditor_Label[3],255,255,255)				valasz = 1				guiLabelSetColor(GUIEditor_Label[2],255,200,200)				guiSetFont(GUIEditor_Label[2],"default-bold-small")			end		) 		bindKey ("2", "down", 			function()				guiSetFont(GUIEditor_Label[2],"default-normal")				guiLabelSetColor(GUIEditor_Label[2],255,255,255)				valasz2 = 2				guiLabelSetColor(GUIEditor_Label[3],255,200,200)				guiSetFont(GUIEditor_Label[3],"default-bold-small")			end		)		lejar = setTimer(			function()				ido = ido-1				guiSetText(GUIEditor_Label[4], ido )				if ido == 0 then					szVeg() --so it will be called 30s after szAblak				end			end, 1000,30)	endendfunction szVeg()	if isTimer(lejar) then --is it possible? It's useless i think		killTimer(lejar)	end	szavon = false	guiSetText(GUIEditor_Label[4], "szavazatok számlálása..." )	guiSetFont(GUIEditor_Label[2],"default-normal")	guiSetFont(GUIEditor_Label[3],"default-normal")	guiLabelSetColor(GUIEditor_Label[3],255,255,255)	guiLabelSetColor(GUIEditor_Label[2],255,255,255)	local v2 = valasz2 --if 2 was pressed this will be 2, otherwise 0	triggerServerEvent("submit",getLocalPlayer(),valasz,v2) --valasz will be 1 if 1 key got pressed, otherwise 0	unbindKey ("1", "down", 		function()			guiSetFont(GUIEditor_Label[3],"clear-normal")			guiLabelSetColor(GUIEditor_Label[3],255,255,255)			valasz = 1			guiLabelSetColor(GUIEditor_Label[2],255,200,200)			guiSetFont(GUIEditor_Label[2],"default-bold-small")		end	) 	unbindKey ("2", "down", 		function()			guiSetFont(GUIEditor_Label[2],"default-normal")			guiLabelSetColor(GUIEditor_Label[2],255,255,255)			valasz2 = 2			guiLabelSetColor(GUIEditor_Label[3],255,200,200)			guiSetFont(GUIEditor_Label[3],"default-bold-small")		end	)endaddEvent("szvzas",true) --this will be called once a one player in the server makes a questionaddEventHandler("szvzas",root,	function(question,answ1,answ2) --the handler attached to the root (resourceRoot should also work) accept 3 arguments		szAblak(question,answ1,answ2) --and call the function with these paramenters	end) addEvent("valaszok",true)addEventHandler("valaszok",getRootElement(),function(v1,v2) --after 5 second from szVeg this will be triggered	--v1 1 or 0, v2 2 or 0. BOTH CAN BE 0!!!! ALSO BOTH CAN BE 1 AND 2. The font wans't already set? you are not changing any passed var serverside. This doesn't make sense	guiSetText(GUIEditor_Label[4], "_@/\"" )	guiSetText(GUIEditor_Label[2], guiGetText(GUIEditor_Label[2]) .. " - " .. v1 )	guiSetText(GUIEditor_Label[3], guiGetText(GUIEditor_Label[3]) .. " - " .. v2 )	if v1 > v2 then --i need explanation here. It's error if you are using them to see what localPlayer choose		guiSetFont(GUIEditor_Label[2],"default-bold-small")	elseif v1 < v2 then		guiSetFont(GUIEditor_Label[3],"default-bold-small")	end	setTimer(		function()--reset to default			destroyElement(GUIEditor_Window[1])			szavon = false			ido = 30			valasz = 0			valasz2 = 0		end,	5000, 1)end)

Server


addEvent( "submit", true )addEventHandler( "submit", root,	function (v1, v2) -- 1 or 0, 2 or 0 depending on keypress		setTimer(			function()				triggerClientEvent(root, "valaszok", resourceRoot, v1, v2)			end, 		5000, 1)  	end)function szvzas(playerSource, cmd, theQuestion, theFirstAnswer, theSecondAnswer) --read the wiki for more infos	triggerClientEvent ( root, "szvzas", resourceRoot, theQuestion, theFirstAnswer, theSecondAnswer ) --trigger on root, event "szvzas", source is the resource, and there are 3 argumentsendaddCommandHandler ( "k", szvzas )

Check the comments, starting from the cmdHandler serverside, following the functions. Like if you were the computer

 

Even if clients check if a "poll" is ongoing, wouldn't be better to make the check serverside? less events triggered means less bandwidth used, more reliable, more secure, much faster. I'm talking about szavon

What are valasz and valasz2 used for? check my comments. I can't understand the part

 

Why is the event submit serverside? it will be spammed: every player will trigger it, and for every trigger it will trigger another event to the whole server!!!

 

let's start with these. PS keep in mind the commandhandler of @Tails because once we get this working we will use it

now, when i press 1, first answer - 1, second - 0, but! when i press 2, the first answer - 1 (o.O), and second - 2.. Why? :S

Link to comment
2 hours ago, LoPollo said:
1 hour ago, Turbo777 said:

now, when i press 1, first answer - 1, second - 0, but! when i press 2, the first answer - 1 (o.O), and second - 2.. Why? :S

I was talking about this when i said:

2 hours ago, LoPollo said:

--v1 1 or 0, v2 2 or 0. BOTH CAN BE 0!!!! ALSO BOTH CAN BE 1 AND 2

 

 

If you have time the best thing may be rewrite the "logic" part of the code:

server:

  • The variable about the poll (question, answer1, answer2) should be here. These variables are passed then to the clients on vote start to let them vote.
  • The check to prevent 2 votes ongoing should be there.
  • It should accept clients votes with an event. The number of votes for one or other answer are handled serverside.
  • It should send to clients the numbers of votes each answer have every time a new vote is made

client:

  • All the gui part should (must) be here (lol). You already have this part, with some changes you can adapt the existing one.
  • The vote the player make is handled here, and then sent to server. You should think if you want players to have the possibility to change or remove their vote.

 

The base of a possible new script is there. If you don't want to completely rewrite the script then big changes must be made, a rewrite could be "cleaner". Also remember to write code using english: i'm not talking about chatbox or similar, i'm talking about functions names and variables. Last but not least, in case you don't already, the names of variables and functions should be as more code-related as possible ( e.g. if i say getPlayerName i think about a function that return the name of a player element ).

 

If you don't want to rewrite, a lot of small steps must be made... and i'm not good with lots of small things. If you find yourself more comfortable this way, then do this way, i'm not giving "orders" :)

Edited by LoPollo
Link to comment

I think than an example may give an idea.

This is the serverside. IT'S ONLY AN EXAMPLE; PLUS IT'S UNTESTED. THERE MAY BE ERRORS AND MISSING THINGS

local playersVotes = {} --playersVotes.player = 1 or 2 (see below) or whatever, event true, false and nil can be used
local poll = {} --poll.question, poll.answers[1 or 2 or some index, up to you] (poll.answers is a table)

addEvent( "onClientVote", true ) --when clients make vote, they will trigger this event


--!
--! @brief      Starts a poll.
--!
--! @param      playerSource  The player who started the poll, the one who typed /k q; a1; a2(;)
--! @param      cmd           The command
--!
--! @return     None. But it will let players know that a poll is started using onPollStart
--!
function startPoll( playerSource, cmd, ... )
	--infos about ... and arg are available here: https://www.lua.org/pil/5.2.html
	if poll.question ~= nil then --or poll ~= {}, or similar. Make sure every poll request while a poll is ongoing is ignored
		local splitSeparator = "; "
		local argTable = split( table.concat(arg," "), splitSeparator) --recreate the full line with concat, then separate question from the 2 answers using split
		if #argTable < 3 then
			outputChatBox("Not enough arguments supplied")
		else
			--unpack can also be used if you want. Fill the poll infos
			poll.question = argTable[1]
			poll.answers = { argTable[2], argTable[3] }

			triggerClientEvent( root, "onPollStart", resourceRoot, poll.question, poll.answers ) --Clients! The poll is started!

			poll.timer = setTimer( endPoll, 30*1000, 1 ) --end the poll
		end
	else
		outputChatBox( "A poll is already ongoing!", playerSource, 255, 0, 0 ) --yes client, you got ignored :P
	end
end
addCommandHandler( "k", startPoll )

--!
--! @brief      Triggered from clients when they vote.
--!
--! @param      theVotedAnswer  The index of the answer the client has chose
--!
--! @return     None. But will trigger onVoteUpdate to let players know the vote each answer has after each vote is made
--!
function handleClientVote(theVotedAnswer) --pass the index of the answer, you can pass nil to remove the vote
	local thePlayer = client --easier to read lol, everyone has its own style
	playersVotes.thePlayer = theVotedAnswer --keep track of the vote
	triggerClientEvent( root, "onVoteUpdate", resourceRoot, countVotes() ) --count them and let the players know the count
end
addEventHandler( "onClientVote", root, handleClientVote )


--!
--! @brief      Counts the number of votes each answer has.
--!
--! @return     A table: [1] number of votes for first answer, [2] number of votes for second answer.
--!
function countVotes()
	local votesCount = {}
	for thePlayerWhoVoted,theVotedAnswer in pairs(playersVotes) do
		--assuming theVotedAnswer is the index 1 or 2, as Descrition says
		votesCount[theVotedAnswer] = votesCount[theVotedAnswer] + 1 --sum all votes
	end
	return votesCount
end


--!
--! @brief      Called when a poll ends. Reset to default the variables
--!
--! @return     None. But triggers onPollEnd to the clients, informing them about the results
--!
function endPoll()
	--Here players will get the gui hidden/disabled/whatever you need. Makes them unable to vote.
	--Also inform them with the result of the poll. Remember that's a table with 1 and 2 as index
	triggerClientEvent( root, "onVoteUpdate", resourceRoot, countVotes() )
	triggerClientEvent( root, "onPollEnd", resourceRoot ) 
	poll = {}
	playersVotes = {}
end


--
--In clientside, you must implement:
--onPollStart -> show the gui
--onVoteUpdate -> update votes, so players know how poll is going and which answer is "winning"
--onPollEnd -> disable the gui, players MUST NOT vote anymore. Hide completely the gui after some time if you decide only to disable it to let the results be shown there
--

I tried to explain as much as i could, let me know if you have doubts or questions. If you want to work on the already existing script, we're still here. If you want to rewrite it, this can help you with the start.

  • Like 1
Link to comment

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