Jump to content

How to fix this bug in this script?


Turbe$Z

Recommended Posts

When me and my friend press '2', the script show double answer (- 1 - 1 instead of -2) or when me press '1' and my friend press '2', the script show this: 1.) Answer1 - 1 - 0 and 2.) Answer2 - 1 - 0.. How to fix this? Sorry for my bad english. :S

code:

--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 = 1
				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 v1 = valasz --if 2 was pressed this will be 2, otherwise 0
	local v2 = valasz2 --if 2 was pressed this will be 2, otherwise 0
	triggerServerEvent("submit",getLocalPlayer(),v1,v2) --valasz will be 1 if 1 key got pressed, otherwise 0

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 )

 

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