GTX Posted February 14, 2012 Posted February 14, 2012 Hello, I got this addEvent( 'onAccountsSend',true ) local labels = { } addEventHandler( 'onAccountsSend',root, function( t ) for index, data in ipairs( t ) do if labels[ index ] == nil or labels[ index ] == false then labels[ index ] = guiCreateColorLabel(13,20 * index + 39,500,276,'#ffffff#'..tostring( index )..": "..tostring( data.account ),false,tab_leaderboard ) else guiSetText(labels[ index ], '#ffffff#'..tostring( index )..": "..tostring( data.account )) end if index == 10 then break end end end ) Everytime when I click on button, script creates new label.
Kenix Posted February 14, 2012 Posted February 14, 2012 addEvent( 'onAccountsSend',true ) local labels = { } addEventHandler( 'onAccountsSend',root, function( t ) for index, data in ipairs( t ) do if isElement( labels[ index ] ) then destroyElement( labels[ index ] ) end labels[ index ] = guiCreateColorLabel( 13,20 * index + 39,500,276,'#ffffff#'..tostring( index )..": "..tostring( data.account ),false,tab_leaderboard ) if index == 10 then break end end end )
GTX Posted February 14, 2012 Author Posted February 14, 2012 Still the same problem, when I click on the button twice it creates two labels
Kenix Posted February 14, 2012 Posted February 14, 2012 Can you give me full code? I test it and fix. via pm.
Kenix Posted February 14, 2012 Posted February 14, 2012 You have syntax problem with 'end'. Your code function theButtons() if source == btn_show_points then triggerServerEvent("recievePrefix", localPlayer, "Points") end addEventHandler ( "onClientGUIClick", btn_show_points, theButtons ) Correct function theButtons() if source == btn_show_points then triggerServerEvent("recievePrefix", localPlayer, "Points") end end addEventHandler ( "onClientGUIClick", btn_show_points, theButtons ) Tabulate code and you can see where you forgot 'end'.
Kenix Posted February 14, 2012 Posted February 14, 2012 (edited) addEvent( 'onAccountsSend',true ) local labels = { } addEventHandler( 'onAccountsSend',root, function( t ) if #labels == 0 then for index, data in ipairs( t ) do guiCreateColorLabel( 13,20 * index + 39,500,276,'#ffffff#'..tostring( index )..": "..tostring( data.account ),false,tab_leaderboard ) if index == 10 then break end end labels[ 1 ] = true end end ) I tested your resource. It working. Edited February 14, 2012 by Guest
Castillo Posted February 14, 2012 Posted February 14, 2012 You have syntax problem with 'end'.Your code function theButtons() if source == btn_show_points then triggerServerEvent("recievePrefix", localPlayer, "Points") end addEventHandler ( "onClientGUIClick", btn_show_points, theButtons ) Correct function theButtons() if source == btn_show_points then triggerServerEvent("recievePrefix", localPlayer, "Points") end end addEventHandler ( "onClientGUIClick", btn_show_points, theButtons ) Tabulate code and you can see where you forgot 'end'. I don't know why you need the 'if' check. function theButtons() triggerServerEvent("recievePrefix", localPlayer, "Points") end addEventHandler ( "onClientGUIClick", btn_show_points, theButtons, false )
Kenix Posted February 14, 2012 Posted February 14, 2012 My bad not see it. But anyway it should work. Tested.
GTX Posted February 14, 2012 Author Posted February 14, 2012 Okay, now I got another problem. I created more buttons and I don't know how to change text... Maybe I need to destroy and create it again. Client: btn_show_points = guiCreateButton(349,47,114,36,"Points",false) btn_show_cash = guiCreateButton(474,47,114,36,"Cash",false) btn_show_wins = guiCreateButton(349,97,114,36,"Wins",false) btn_show_ttset = guiCreateButton(474,97,114,36,"Top times set",false) btn_show_played = guiCreateButton(349,47,114,36,"Maps played",false) btn_show_deaths = guiCreateButton(474,47,114,36,"Deaths",false) function theButtons () if source == btn_show_points then triggerServerEvent("recievePrefix", localPlayer, "points") end if source == btn_show_cash then triggerServerEvent("recievePrefix", localPlayer, "money_q") end if source == btn_show_ttset then triggerServerEvent("recievePrefix", localPlayer, "ttset") end if source == btn_show_played then triggerServerEvent("recievePrefix", localPlayer, "mapsplayed") end if source == btn_show_deaths then triggerServerEvent("recievePrefix", localPlayer, "deaths") end end addEventHandler ( "onClientGUIClick", btn_show_points, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_cash, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_ttset, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_played, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_deaths, theButtons ) addEvent( 'onAccountsSend',true ) local labels = { } addEventHandler( 'onAccountsSend',root, function( t ) if #labels == 0 then for index, data in ipairs( t ) do guiCreateColorLabel( 13,20 * index + 39,500,276,'#ffffff#'..tostring( index )..": "..tostring( data.account ),false ) if index == 10 then break end end labels[ 1 ] = true end end ) Server: function sortAccounts(prefix) local rowdata = { } for _, account in pairs( getAccounts( ) ) do rowdata[ #rowdata + 1 ] = { account = getAccountData( account, "playername" ), points = getAccountData( account, tostring(prefix) ) } end table.sort( rowdata, function ( a, b ) return ( tonumber( a.points ) or 0 ) > ( tonumber( b.points ) or 0 ) end ) return rowdata end function tables(prefix) for index, data in ipairs( sortAccounts( ) ) do if index == 3 then break end end triggerClientEvent( 'onAccountsSend',root,sortAccounts(prefix) ) end addEvent("recievePrefix", true) addEventHandler("recievePrefix", root, tables) Now I think this is already impossible lol...
Castillo Posted February 14, 2012 Posted February 14, 2012 Try this: btn_show_points = guiCreateButton(349,47,114,36,"Points",false) btn_show_cash = guiCreateButton(474,47,114,36,"Cash",false) btn_show_wins = guiCreateButton(349,97,114,36,"Wins",false) btn_show_ttset = guiCreateButton(474,97,114,36,"Top times set",false) btn_show_played = guiCreateButton(349,47,114,36,"Maps played",false) btn_show_deaths = guiCreateButton(474,47,114,36,"Deaths",false) function theButtons () if source == btn_show_points then triggerServerEvent("recievePrefix", localPlayer, "points") end if source == btn_show_cash then triggerServerEvent("recievePrefix", localPlayer, "money_q") end if source == btn_show_ttset then triggerServerEvent("recievePrefix", localPlayer, "ttset") end if source == btn_show_played then triggerServerEvent("recievePrefix", localPlayer, "mapsplayed") end if source == btn_show_deaths then triggerServerEvent("recievePrefix", localPlayer, "deaths") end end addEventHandler ( "onClientGUIClick", btn_show_points, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_cash, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_ttset, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_played, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_deaths, theButtons ) addEvent( 'onAccountsSend',true ) local labels = { } addEventHandler( 'onAccountsSend',root, function( t ) for index, label in pairs(labels) do if isElement(label) then destroyElement(label) end end if #labels == 0 then for index, data in ipairs( t ) do labels[index] = guiCreateColorLabel( 13,20 * index + 39,500,276,'#ffffff#'..tostring( index )..": "..tostring( data.account ),false ) if index == 10 then break end end end end )
Kenix Posted February 14, 2012 Posted February 14, 2012 guiCreateColorLabel,is return element in your script?
GTX Posted February 14, 2012 Author Posted February 14, 2012 This code doesn't work. I think guiCreateColorLabel doesn't return element. function guiCreateColorLabel(ax, ay, bx, by,str, parent, bool) local pat = "(.-)#(%x%x%x%x%x%x)" local s, e, cap, col = str:find(pat, 1) local last = 1 while s do if cap == "" and col then r,g,b = tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(5, 6)) end if s ~= 1 or cap ~= "" then local w = dxGetTextWidth(cap) lbl = guiCreateLabel(ax, ay, ax + w, by,cap,parent, bool) if r == nil then r = 255 end if g == nil then g = 255 end if b == nil then b = 255 end guiLabelSetColor(lbl,r,g,b) ax = ax + w r,g,b = tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(5, 6)) end last = e + 1 s, e, cap, col = str:find(pat, last) end if last <= #str then cap = str:sub(last) local w = dxGetTextWidth(cap) lbl2 = guiCreateLabel(ax, ay, ax + w, by,cap,parent, bool) guiLabelSetColor(lbl2,r,g,b) end end
Castillo Posted February 14, 2012 Posted February 14, 2012 It wouldn't return just one element, as it's creating many labels, try this: btn_show_points = guiCreateButton(349,47,114,36,"Points",false) btn_show_cash = guiCreateButton(474,47,114,36,"Cash",false) btn_show_wins = guiCreateButton(349,97,114,36,"Wins",false) btn_show_ttset = guiCreateButton(474,97,114,36,"Top times set",false) btn_show_played = guiCreateButton(349,47,114,36,"Maps played",false) btn_show_deaths = guiCreateButton(474,47,114,36,"Deaths",false) function theButtons () if source == btn_show_points then triggerServerEvent("recievePrefix", localPlayer, "points") end if source == btn_show_cash then triggerServerEvent("recievePrefix", localPlayer, "money_q") end if source == btn_show_ttset then triggerServerEvent("recievePrefix", localPlayer, "ttset") end if source == btn_show_played then triggerServerEvent("recievePrefix", localPlayer, "mapsplayed") end if source == btn_show_deaths then triggerServerEvent("recievePrefix", localPlayer, "deaths") end end addEventHandler ( "onClientGUIClick", btn_show_points, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_cash, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_ttset, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_played, theButtons ) addEventHandler ( "onClientGUIClick", btn_show_deaths, theButtons ) addEvent( 'onAccountsSend',true ) local labels = { } addEventHandler( 'onAccountsSend',root, function( t ) for index, lbls in pairs(labels) do for index, label in ipairs(lbls) do if isElement(label) then destroyElement(label) end end end labels = { } if #labels == 0 then for index, data in ipairs( t ) do labels[index] = guiCreateColorLabel( 13,20 * index + 39,500,276,'#ffffff#'..tostring( index )..": "..tostring( data.account ),false ) if index == 10 then break end end end end ) function guiCreateColorLabel(ax, ay, bx, by,str, parent, bool) local pat = "(.-)#(%x%x%x%x%x%x)" local s, e, cap, col = str:find(pat, 1) local last = 1 local labels = {} while s do if cap == "" and col then r,g,b = tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(5, 6)) end if (s ~= 1) or cap ~= "" then local w = dxGetTextWidth(cap) lbl = guiCreateLabel(ax, ay, ax + w, by,cap,parent, bool) table.insert(labels, lbl) if (r == nil) then r = 255 end if (g == nil) then g = 255 end if (b == nil) then b = 255 end guiLabelSetColor(lbl,r,g,b) ax = ax + w r,g,b = tonumber("0x"..col:sub(1, 2)), tonumber("0x"..col:sub(3, 4)), tonumber("0x"..col:sub(5, 6)) end last = e + 1 s, e, cap, col = str:find(pat, last) end if (last <= #str) then cap = str:sub(last) local w = dxGetTextWidth(cap) lbl2 = guiCreateLabel(ax, ay, ax + w, by,cap,parent, bool) table.insert(labels, lbl2) guiLabelSetColor(lbl2,r,g,b) end return labels end
GTX Posted February 14, 2012 Author Posted February 14, 2012 When I press the first button works fine, but when I press any other button the label destroys
Castillo Posted February 14, 2012 Posted February 14, 2012 I don't understand what do you mean, it should destroy the labels when you click another button, and create them again with the new information.
GTX Posted February 14, 2012 Author Posted February 14, 2012 I don't understand what do you mean, it should destroy the labels when you click another button, and create them again with the new information. Yes, but it doesn't create them, that's the problem
Castillo Posted February 14, 2012 Posted February 14, 2012 Copy the script again and tell me if it creates them now.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now