smokerhands

Can't add rows to a GUI automatically

Recommended Posts

Hello I started scripting fairly recently and I'm having a litle problem with my script, let me explain. Instead of adding rows to the GUI manually and causing a huge mess, I would like to get them added/removed depending on the lines I have on a server sided table. Let me show you the files:

 

Server Side:
 

Spoiler

addEvent("setWalkStyle",true)

walkStyleTableID = {
	[118] = {118, "Man"},
	[119] = {119, "Shuffle"},
    [120] = {120, "Old Man"},
    [121] = {121, "Gangster 1"},
    [122] = {122, "Gangster 2"},
    [123] = {123, "Old Fat Man"},
    [124] = {124, "Fat Man"},
    [125] = {125, "Jogger"},
    [126] = {126, "Drunk"},
    [127] = {127, "Blind"},
    [128] = {128, "SWAT"},
	[129] = {129, "Woman"},
	[131] = {131, "Busy Woman"},
	[132] = {132, "Sexy Woman"},
    [133] = {133, "Hooker"},
    [134] = {134, "Old Woman"},
    [135] = {135, "Fat Woman"},
    [136] = {136, "Jogging Woman"},
}

function setWalk (thePlayer, cmd, walkstyle)
    walkstyle = tonumber (walkstyle)
	local target = exports['roleplay-accounts']:getPlayerFromPartialName(name, player)
    if walkstyle == nil then
    return
    elseif walkStyleTableID[walkstyle ] == nil then 
    outputChatBox ("Invalid ID", target)
    return 
	end
    setPedWalkingStyle (thePlayer, walkStyleTableID[walkstyle][1])
    outputChatBox ("Your walking style was set to " .. walkStyleTableID[walkstyle][2] )
end

addCommandHandler("ws", setWalk)
addEventHandler("setWalkStyle",root,function(sid) setWalk(client,"ws",sid) end)

 

 

Client Side:

Spoiler

addEventHandler("onClientResourceStart", resourceRoot,
    function()
local screenW, screenH = guiGetScreenSize()
        window = guiCreateWindow((screenW - 366) / 2, (screenH - 416) / 2, 366, 416, "Walking Styles", false)
        guiWindowSetSizable(window, false)

        gridlist = guiCreateGridList(29, 41, 308, 320, false, window)
        guiGridListAddColumn(gridlist, "ID", 0.3)
        guiGridListAddColumn(gridlist, "Name", 0.7)
        for i = 1, 18 do
            guiGridListAddRow(gridlist)
        end
        guiGridListSetItemText(gridlist, 0, 1, "118", false, false)
        guiGridListSetItemText(gridlist, 0, 2, "Man", false, false)
        guiGridListSetItemText(gridlist, 1, 1, "119", false, false)
        guiGridListSetItemText(gridlist, 1, 2, "Shuffle", false, false)
        guiGridListSetItemText(gridlist, 2, 1, "120", false, false)
        guiGridListSetItemText(gridlist, 2, 2, "Old Man", false, false)
        guiGridListSetItemText(gridlist, 3, 1, "121", false, false)
        guiGridListSetItemText(gridlist, 3, 2, "Gangster 1", false, false)
        guiGridListSetItemText(gridlist, 4, 1, "122", false, false)
        guiGridListSetItemText(gridlist, 4, 2, "Gangster 2", false, false)
        guiGridListSetItemText(gridlist, 5, 1, "123", false, false)
        guiGridListSetItemText(gridlist, 5, 2, "Old Fat Man", false, false)
        guiGridListSetItemText(gridlist, 6, 1, "124", false, false)
        guiGridListSetItemText(gridlist, 6, 2, "Fat Man", false, false)
        guiGridListSetItemText(gridlist, 7, 1, "125", false, false)
        guiGridListSetItemText(gridlist, 7, 2, "Jogger", false, false)
        guiGridListSetItemText(gridlist, 8, 1, "126", false, false)
        guiGridListSetItemText(gridlist, 8, 2, "Drunk", false, false)
        guiGridListSetItemText(gridlist, 9, 1, "127", false, false)
        guiGridListSetItemText(gridlist, 9, 2, "Blind", false, false)
        guiGridListSetItemText(gridlist, 10, 1, "128", false, false)
        guiGridListSetItemText(gridlist, 10, 2, "SWAT", false, false)
        guiGridListSetItemText(gridlist, 11, 1, "129", false, false)
        guiGridListSetItemText(gridlist, 11, 2, "Woman", false, false)
        guiGridListSetItemText(gridlist, 12, 1, "131", false, false)
        guiGridListSetItemText(gridlist, 12, 2, "Busy Woman", false, false)
        guiGridListSetItemText(gridlist, 13, 1, "132", false, false)
        guiGridListSetItemText(gridlist, 13, 2, "Sexy Woman", false, false)
        guiGridListSetItemText(gridlist, 14, 1, "133", false, false)
        guiGridListSetItemText(gridlist, 14, 2, "Hooker", false, false)
        guiGridListSetItemText(gridlist, 15, 1, "134", false, false)
        guiGridListSetItemText(gridlist, 15, 2, "Old Woman", false, false)
        guiGridListSetItemText(gridlist, 16, 1, "135", false, false)
        guiGridListSetItemText(gridlist, 16, 2, "Fat Woman", false, false)
        guiGridListSetItemText(gridlist, 17, 1, "136", false, false)
        guiGridListSetItemText(gridlist, 17, 2, "Jogging Woman", false, false)
        acceptbtn = guiCreateButton(39, 373, 126, 33, "Accept", false, window)
        closebtn = guiCreateButton(201, 373, 126, 33, "Close", false, window)  

		guiSetVisible(window, false)
		
		addEventHandler("onClientGUIClick", closebtn, closemenu, false) 
		addEventHandler("onClientGUIClick", acceptbtn, acceptanim, false) 		

    end
)

	function closemenu(wid)
		guiSetVisible(window, false)
		showCursor(false)  
	end

	function acceptanim()
		local selectedRow, selectedCol = guiGridListGetSelectedItem(gridlist)
		sid = guiGridListGetItemText( gridlist, selectedRow, selectedCol)
		triggerServerEvent("setWalkStyle",localPlayer,sid)
			guiSetVisible(window, false)
			showCursor(false)  
		
	end
	
	addCommandHandler("s", function()
	guiSetVisible(window, true)
	showCursor(true)   
	end)

 

 

Screenshot:

Spoiler

W8B6NEv.png

 

Share this post


Link to post
 
addEventHandler("onClientResourceStart", resourceRoot,
    function()
local screenW, screenH = guiGetScreenSize()
        window = guiCreateWindow((screenW - 366) / 2, (screenH - 416) / 2, 366, 416, "Walking Styles", false)
        guiWindowSetSizable(window, false)

        gridlist = guiCreateGridList(29, 41, 308, 320, false, window)
        guiGridListAddColumn(gridlist, "ID", 0.3)
        guiGridListAddColumn(gridlist, "Name", 0.7)
        for i = 1, 18 do
           row= guiGridListAddRow(gridlist)
        end
        guiGridListSetItemText(gridlist, row, 1, "118", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Man", false, false)
        guiGridListSetItemText(gridlist, row, 1, "119", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Shuffle", false, false)
        guiGridListSetItemText(gridlist, row, 1, "120", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Old Man", false, false)
        guiGridListSetItemText(gridlist, row, 1, "121", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Gangster 1", false, false)
        guiGridListSetItemText(gridlist, row, 1, "122", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Gangster 2", false, false)
        guiGridListSetItemText(gridlist, row, 1, "123", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Old Fat Man", false, false)
        guiGridListSetItemText(gridlist, row, 1, "124", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Fat Man", false, false)
        guiGridListSetItemText(gridlist, row, 1, "125", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Jogger", false, false)
        guiGridListSetItemText(gridlist, row, 1, "126", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Drunk", false, false)
        guiGridListSetItemText(gridlist, row, 1, "127", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Blind", false, false)
        guiGridListSetItemText(gridlist, row, 1, "128", false, false)
        guiGridListSetItemText(gridlist, row, 2, "SWAT", false, false)
        guiGridListSetItemText(gridlist, row, 1, "129", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Woman", false, false)
        guiGridListSetItemText(gridlist, row, 1, "131", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Busy Woman", false, false)
        guiGridListSetItemText(gridlist, row, 1, "132", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Sexy Woman", false, false)
        guiGridListSetItemText(gridlist, row, 1, "133", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Hooker", false, false)
        guiGridListSetItemText(gridlist, row, 1, "134", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Old Woman", false, false)
        guiGridListSetItemText(gridlist, row, 1, "135", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Fat Woman", false, false)
        guiGridListSetItemText(gridlist, row, 1, "136", false, false)
        guiGridListSetItemText(gridlist, row, 2, "Jogging Woman", false, false)
        acceptbtn = guiCreateButton(39, 373, 126, 33, "Accept", false, window)
        closebtn = guiCreateButton(201, 373, 126, 33, "Close", false, window)  

		guiSetVisible(window, false)
		
		addEventHandler("onClientGUIClick", closebtn, closemenu, false) 
		addEventHandler("onClientGUIClick", acceptbtn, acceptanim, false) 		

    end
)

	function closemenu(wid)
		guiSetVisible(window, false)
		showCursor(false)  
	end

	function acceptanim()
		local selectedRow, selectedCol = guiGridListGetSelectedItem(gridlist)
		sid = guiGridListGetItemText( gridlist, selectedRow, selectedCol)
		triggerServerEvent("setWalkStyle",localPlayer,sid)
			guiSetVisible(window, false)
			showCursor(false)  
		
	end
	
	addCommandHandler("s", function()
	guiSetVisible(window, true)
	showCursor(true)   
	end)

 

Edited by Dimos7

Share this post


Link to post

That's one way of doing it. One friend of mine told me something about using 

for i = 1, 18 do

I could do 

for i = 1, #tablename do

but for some reason I can't figure it out and he did no develop much

Share this post


Link to post

Create a new lua file and paste this:

walkStyleTable = {
	[118] = "Man",
	[119] = "Shuffle",
    [120] = "Old Man",
    [121] = "Gangster 1",
    [122] = "Gangster 2",
    [123] = "Old Fat Man",
    [124] = "Fat Man",
    [125] = "Jogger",
    [126] = "Drunk",
    [127] = "Blind",
    [128] = "SWAT",
	[129] = "Woman",
	[131] = "Busy Woman",
	[132] = "Sexy Woman",
    [133] = "Hooker",
    [134] = "Old Woman",
    [135] = "Fat Woman",
    [136] = "Jogging Woman",
}

In the meta.xml set the type of this lua file to "shared"

Client:

local screenW, screenH = guiGetScreenSize()

addEventHandler("onClientResourceStart", resourceRoot,
    function()
        window = guiCreateWindow((screenW - 366) / 2, (screenH - 416) / 2, 366, 416, "Walking Styles", false)
        guiWindowSetSizable(window, false)

        gridlist = guiCreateGridList(29, 41, 308, 320, false, window)
        guiGridListAddColumn(gridlist, "ID", 0.3)
        guiGridListAddColumn(gridlist, "Name", 0.6)
		
		for id, name in pairs(walkStyleTable) do
			row = guiGridListAddRow(gridlist)
			guiGridListSetItemText(gridlist, row, 1, id, false, false)
			guiGridListSetItemText(gridlist, row, 2, name, false, false)
		end
		
        acceptbtn = guiCreateButton(39, 373, 126, 33, "Accept", false, window)
        closebtn = guiCreateButton(201, 373, 126, 33, "Close", false, window)  

		guiSetVisible(window, false)
		
		addEventHandler("onClientGUIClick", closebtn, closemenu, false) 
		addEventHandler("onClientGUIClick", acceptbtn, acceptanim, false) 		
    end
)

function closemenu()
	guiSetVisible(window, false)
	showCursor(false)  
end


function acceptanim()
	local selectedRow = guiGridListGetSelectedItem(gridlist)
	local selectedID = guiGridListGetItemText(gridlist, selectedRow, 1)
	triggerServerEvent("setWalkStyle",localPlayer,localPlayer,tonumber(selectedID))
	closemenu()
end
	
addCommandHandler("s", function()
	guiSetVisible(window, true)
	showCursor(true)   
end)

Server:

addEvent("setWalkStyle", true)

function setWalk(thePlayer, walkstyle)
	if not walkstyle or type(walkstyle) ~= "number" then return false end
	
	if not walkStyleTable[walkstyle] then
		outputChatBox("Invalid ID", thePlayer)
		return false
	else
		setPedWalkingStyle(thePlayer, walkstyle)
		outputChatBox("Your walking style was set to " .. tostring(walkstyle), thePlayer)
    	return true
	end
end
addEventHandler("setWalkStyle", root, setWalk)

I don't know why does the sorting of the table messes up, but it works.

Edited by Dzsozi (h03)

Share this post


Link to post

I don’t know what this could mean. Sometimes when you paste a code from the forum there is a character after the line, which doesn’t show up, but if you hit backspace at the end of that line, it deletes something and the code works. Try doing the same at the end of the 12th line, hit a backspace. Or try copying the code with the little icon in the top left corner, if this is not how you did it.

I didn’t have any errors when I was testing so this must be the problem. I don’t know why it happens.

Edited by Dzsozi (h03)
  • Thanks 1

Share this post


Link to post

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.