Jump to content

How To Get the Selected Item on a Gridlist?


Recommended Posts

Hello.

I have a script that creates kind-of a "Admin" GUI. I have the Interface set how I want it, but I'm having trouble getting the selected item in the gridlist. Here is my script:

function initGUI()
-- Creates the Parents
   GUIEditor_Window = {}
   GUIEditor_Button = {}
   GUIEditor_Grid = {}
 
-- Window
   GUIEditor_Window[1] = guiCreateWindow(108,174,450,333,"ElexTronic Punish Panel",false)
 
-- Gridlist
   playerList = guiCreateGridList(19,36,113,277,false,GUIEditor_Window[1])
guiGridListSetSelectionMode(playerList,2)
 
-- Fills the Gridlist With Players
local column = guiGridListAddColumn(playerList,"Player",0.2)
if column then
for id, player in ipairs(getElementsByType("player")) do
local row = guiGridListAddRow ( playerList )
guiGridListSetItemText ( playerList, row, column, getPlayerName ( player ), false, false )
guiGridListSetItemData ( playerList, row, column, getPlayerName( player ) )
end
end
 
-- Creates the Buttons
   GUIEditor_Button[1] = guiCreateButton(206,35,216,31,"Slap Him Into Last Week",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], slapPlayer, false )
   GUIEditor_Button[2] = guiCreateButton(206,76,216,31,"Shoot Him Out Of His Vehicle",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], ejectPlayer, false )
   GUIEditor_Button[3] = guiCreateButton(206,119,216,31,"Remove All His Weapons",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], unarmPlayer, false )
   GUIEditor_Button[4] = guiCreateButton(206,166,216,31,"Third-Degree Burn Him",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], burnPlayer, false )
   GUIEditor_Button[5] = guiCreateButton(206,210,216,31,"Gooch Choke Him",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], chokePlayer, false )
   GUIEditor_Button[6] = guiCreateButton(206,257,216,31,"Warp Him",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], warpPlayer, false )
end
addEventHandler("onClientResourceStart", getResourceRootElement( getThisResource() ), initGUI )
 
function burnPlayer( button )
if button == "left" then
local row, col = guiGridListGetSelectedItem ( playerList )
local selected = guiGridListGetItemData ( playerList, row, col )
setPedOnFire( selected, true )
end
end

I know how to add the functions to execute the command, but I don't know how to get the Player from the gridlist to use the command on. Please help me solve this.

Thanks in advance.

EDIT: I have edited the code a little and added the burnPlayer function to demonstrate my problem. Every time I try to burn a player using the button, it gives me: Bad argument @ 'setPedOnFire'

To my understanding by this, selected was passed an invalid argument, therefore being false.

Link to comment

The data assigned to grid list items can only be string as you know same goes for what is returned when you try to get its data... So, what your selected variable will be is the player name. You can't pass player name to setPedOnFire! You first need to getPlayerFromName and then pass that player to setPedOnFire BUT do it server-side so that the player will die because I doubt client-side setPedOnFire will take any effects on remote players.

Link to comment

Thanks! It works now!

server.lua

function burnPlayer ( player )
setPedOnFire( player, true )
end
addEvent( "burn", true )
addEventHandler( "burn", getRootElement(), burnPlayer )

client.lua

function initGUI()
-- Creates the Parents
   GUIEditor_Window = {}
   GUIEditor_Button = {}
   GUIEditor_Grid = {}
 
-- Window
   GUIEditor_Window[1] = guiCreateWindow(108,174,450,333,"ElexTronic Punish Panel",false)
 
-- Gridlist
   playerList = guiCreateGridList(19,36,113,277,false,GUIEditor_Window[1])
guiGridListSetSelectionMode(playerList,2)
 
-- Fills the Gridlist With Players
local column = guiGridListAddColumn(playerList,"Player", 1)
if column then
for id, player in ipairs(getElementsByType("player")) do
local row = guiGridListAddRow ( playerList )
guiGridListSetItemText ( playerList, row, column, getPlayerName ( player ), false, false )
guiGridListSetItemData ( playerList, row, column, getPlayerName ( player ) )
end
end
 
-- Creates the Buttons
   GUIEditor_Button[1] = guiCreateButton(206,35,216,31,"Slap Him Into Last Week",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], slapPlayer, false )
   GUIEditor_Button[2] = guiCreateButton(206,76,216,31,"Shoot Him Out Of His Vehicle",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], ejectPlayer, false )
   GUIEditor_Button[3] = guiCreateButton(206,119,216,31,"Remove All His Weapons",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], unarmPlayer, false )
   GUIEditor_Button[4] = guiCreateButton(206,166,216,31,"Third-Degree Burn Him",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], burnPlayer, false )
   GUIEditor_Button[5] = guiCreateButton(206,210,216,31,"Gooch Choke Him",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], chokePlayer, false )
   GUIEditor_Button[6] = guiCreateButton(206,257,216,31,"Warp Him",false,GUIEditor_Window[1])
addEventHandler ( "onClientGUIClick", GUIEditor_Button[4], warpPlayer, false )
end
addEventHandler("onClientResourceStart", getResourceRootElement( getThisResource() ), initGUI )
 
function burnPlayer( button )
if button == "left" then
local row, col = guiGridListGetSelectedItem ( playerList )
local selected = guiGridListGetItemData ( playerList, row, col )
outputChatBox( tostring(selected))
local nName = getPlayerFromName( selected )
triggerServerEvent("burn", nName, nName )
end
end

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