Jump to content

What wrong in this vote script?


Turbe$Z

Recommended Posts

6 minutes ago, LoPollo said:

 

There's a security problem here. Yes, source is the player that triggered the event. But it's better to use the hidden variable "client".

The 4th argument is the function that handles the keypress of the bound key (basically the function that is called when you press the key).


function someFunction(firstArgument)	--codeend--inside some event--when using bindkey, the player who triggered the event ("client") will have its "1" key calling "someFunc" with "someArgument" when pressedbindKey(client, "1", "down", someFunction, someArgument)--examplefunction someFunction(sSomeString)	outputDebugString( "Some function has to say something: "..sSomeString )endfunction submit()	 bindKey(client, "1", "down", someFunction, "key 1 was pressed")	 bindKey(client, "2", "down", someFunction, "key 2 was pressed")endaddEvent( "submit", true )addEventHandler( "submit", root, submit )--When a player execute triggerServerEvent on "submit" event, the function "submit" will be executed:--key 1 and key 2 will be bound to "someFunction". --So if i press 1 some text will be displayed:--"Some function has to say something: key 1 was pressed"--And if key 2 is pressed another text gets outputted:--"Some function has to say something: key 2 was pressed"

 

i tried this too, and its working, but just v1

--server--

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

--client--

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
        end, 5000, 1)    
 end)

and v2 has got error:

client.lua:103: attempt to concatenate local 'v2' (a nil value)
wtf..

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

I think that when you trigger "submit" event you are not passing the correct variables

Post the line when you trigger the submit event

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)
    triggerServerEvent("submit",getLocalPlayer(),valasz)

 

Link to comment
1 minute ago, MR.S3D said:

show me full code

 

client and server

--client--

local szavon = false
local ido = 30
local valasz = 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)
                valasz = 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 = valasz
	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)
                valasz = 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
        end, 5000, 1)    
 end)

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

 

  • Like 1
Link to comment
2 hours ago, Turbo777 said:

Oh.. Now working, but when i press 2 not add my vote, and when i press 1 add vote to 1 and 2 too.. How to fix this?


	local v2 = valasz
	triggerServerEvent("submit",getLocalPlayer(),valasz,v2) 

 

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

Link to comment
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 = valasz
	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)
                valasz = 2
                guiLabelSetColor(GUIEditor_Label[3],255,200,200)
                guiSetFont(GUIEditor_Label[3],"default-bold-small")
            end)


end

and i tried change "valasz = 2" to "valasz2 = 2" and local v2 = valasz2, but not good :S

Link to comment
On 12/3/2016 at 9:02 PM, Turbo777 said:

The window appear when i type '/k', but i want question, answer1, answer2 in this command (/k Question? Answ1 Answ2 example). But doesn't working.. Sorry, me very noob. :S

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

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