Jump to content

What wrong in this vote script?


Turbe$Z

Recommended Posts

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


        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",getRootElement(),
function(question,answ1,answ2)
 szAblak(question,answ1,answ2)
 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 --

function submit()
        bindKey ("1", "down", 
            function()
                valasz = 1
            end) 
        bindKey ("2", "down", 
            function()
                valasz = 2
            end)
end
addEvent( "submit", true )
addEventHandler( "submit", root, submit )

function szvzas(question,answ1,answ2)
    local question = ""    
	local answ1 = ""
	local answ2 = ""
    triggerClientEvent ( root, "szvzas", root, text) 
end
addCommandHandler ( "k", szvzas, question, answ1, answ2 ) 

 function valaszok(v1,v2)
     triggerClientEvent(root, "valaszok", root, v1, v2)
	 bindKey(player, "1", "down", v1)
	 bindKey(player, "2", "down", v2)
 end

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
And, error: server.lua:20: attempt to concatenate local 'v_1' (a nil value)Why?

Link to comment

client.lua:20: attempt to concatenate local 'v_1' (a nil value)

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

 

Link to comment

error is in line 18, server. You are no passing anything (so it will be nil), the event handler will receive nil and pass that value as v1, when you do local v_1 = nil it will be nil and you concatenate nil

Also remove root as source
PS: next time give me correct informations :( it was not server@20 but client@20

Edited by LoPollo
Link to comment

read what i said it will be "nil" if you do tostring

Also local v_1 = v1 was ok @iPrestege

triggerClientEvent ( root, "szvzas", root, text) 
->
addEventHandler("szvzas",getRootElement(),
function(question,answ1,answ2) --text, nil, nil
 szAblak(question,answ1,answ2) --text, nil, nil
 end)
->
function szAblak(ker,v1,v2) --ker = text, v1 and v2 are nil
->
local v_1 = v1 --both will be nil
local v_2 = v2 --both will be nil

 

Link to comment
6 minutes ago, LoPollo said:

error is in line 18, server. You are no passing anything (so it will be nil), the event handler will receive nil and pass that value as v1, when you do local v_1 = nil it will be nil and you concatenate nil

Also remove root as source
PS: next time give me correct informations :( it was not server@20 but client@20

doesn't working :c warning: server.lua:18: Bad argument @ 'triggerClientEvent' [Expected string at argument 1, got nil]

Link to comment
6 minutes ago, LoPollo said:

Also please note that v_1 and v_2 are useless since there are v1 and v2

take a look here pls

https://wiki.multitheftauto.com/wiki/TriggerClientEvent

 

Like this?

function szvzas(question,answ1,answ2)
    local question = ""    
	local answ1 = ""
	local answ2 = ""
    triggerClientEvent ( source, "szvzas", source, question, answ1, answ2 ) 
end
addCommandHandler ( "k", szvzas, question, answ1, answ2 ) 

 

Link to comment
--server, line 14
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 ) --commandName, handlerFunc
--players will write: /k someQuestion someAnswer1 someAnswer2. Also please note that there MUST BE NO SPACES since arguments are splitted by spaces... do what you want to fix this, i don't know your plans


--client, line 91
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
)

 

  • Like 1
Link to comment

working thanks :D but this why not working? when countdown is 0, the "counting votes" doesn't working.. but why?

--server--

function submit()
        bindKey ("1", "down", 
            function()
                valasz = 1
            end) 
        bindKey ("2", "down", 
            function()
                valasz = 2
            end)
end
addEvent( "submit", true )
addEventHandler( "submit", root, submit )

--client--

function szVeg()
    if isTimer(lejar) then
        killTimer(lejar)
    end
    szavon = false
    guiSetText(GUIEditor_Label[4], "counting votes..." )
    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)


        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

 

Link to comment
3 minutes ago, Turbo777 said:

the "counting votes"

What's that in the script? i guess it's not a function, but you say that's not working so it doesn't make sense

 

3 minutes ago, Turbo777 said:

countdown

What's that in the script? the timer "lejar"?

 

 

explain

Link to comment
triggerServerEvent("submit",getLocalPlayer(),valasz)
-->
function submit()--shouldn't the function accept an argument?
		--also isn't the syntax of bindKey wrong? https://wiki.multitheftauto.com/wiki/BindKey
        bindKey ("1", "down", 
            function()
                valasz = 1
            end) 
        bindKey ("2", "down", 
            function()
                valasz = 2
            end)
end
addEvent( "submit", true )
addEventHandler( "submit", root, submit )

 

What szVeg should do? unbinding and binding keys? what exactly doesn't work? nothing at all? are there errors in the client conosle? are there errors in the server console?

Link to comment
6 minutes ago, LoPollo said:

triggerServerEvent("submit",getLocalPlayer(),valasz)-->function submit()--shouldn't the function accept an argument?		--also isn't the syntax of bindKey wrong? https://wiki.multitheftauto.com/wiki/BindKey        bindKey ("1", "down",             function()                valasz = 1            end)         bindKey ("2", "down",             function()                valasz = 2            end)endaddEvent( "submit", true )addEventHandler( "submit", root, submit )

 

What szVeg should do? unbinding and binding keys? what exactly doesn't work? nothing at all? are there errors in the client conosle? are there errors in the server console?

no errors, just warnings:

server.lua:2: Bad argument @ 'bindKey' [Expected player at argument 1, got string '1']
server.lua:6: Bad argument @ 'bindKey' [Expected player at argument 1, got string '2']

Link to comment
53 minutes ago, Turbo777 said:

no errors, just warnings:

server.lua:2: Bad argument @ 'bindKey' [Expected player at argument 1, got string '1']
server.lua:6: Bad argument @ 'bindKey' [Expected player at argument 1, got string '2']

59 minutes ago, LoPollo said:

--also isn't the syntax of bindKey wrong? https://wiki.multitheftauto.com/wiki/BindKey

 

21 minutes ago, Turbo777 said:

i tried this


function submit()
  bindKey(source, "1", "down")
  bindKey(source, "2", "down")
end
addEvent( "submit", true )
addEventHandler( "submit", root, submit )

but what is the fourth argument?

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

--inside some event
--when using bindkey, the player who triggered the event ("client") will have its "1" key calling "someFunc" with "someArgument" when pressed
bindKey(client, "1", "down", someFunction, someArgument)

--example
function someFunction(sSomeString)
	outputDebugString( "Some function has to say something: "..sSomeString )
end

function submit()
	 bindKey(client, "1", "down", someFunction, "key 1 was pressed")
	 bindKey(client, "2", "down", someFunction, "key 2 was pressed")
end
addEvent( "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"

 

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