Waileer Posted February 2, 2014 Posted February 2, 2014 Hello, I would like to know what should I use to get my script working. If player selects vehicle from grid list and then presses "Buy" it would spawn a vehicle he selected in x,y,z. I think i should use guiGridListGetSelectedItem and createVehicle for server sided script. But I don't know what next, I've folowed Scripting the GUI tutorial on wiki but somehow I don't get it at all, or my script is different. Thanks for answers, regards Waileer.
myonlake Posted February 2, 2014 Posted February 2, 2014 You could post the code here for us to find the problem in your code.
Waileer Posted February 2, 2014 Author Posted February 2, 2014 You could post the code here for us to find the problem in your code. There's no problem in my code, I just don't know how I could make thing I written in main post.
Castillo Posted February 2, 2014 Posted February 2, 2014 Yes, you must use guiGridListGetSelectedItem and guiGridListGetItemText, then to trigger a server side event to create the vehicle, you must use triggerServerEvent.
Waileer Posted February 2, 2014 Author Posted February 2, 2014 Hey again, well theres problem. When I select vehicle, and click button it doesn't spawn. Any thoughts? Client Side: function createVehicleSelection ( ) local sWidth, sHeight = guiGetScreenSize() local Width,Height = 328,537 local X = (sWidth/2) - (Width/2) local Y = (sHeight/2) - (Height/2) windowVehicleShop = guiCreateWindow(X,Y,Width,Height,"Vehicle Shop",false) gridlistVehicleShop = guiCreateGridList(9, 82, 308, 406, false, windowVehicleShop) guiGridListAddColumn(gridlistVehicleShop, "Name", 0.3) guiGridListAddColumn(gridlistVehicleShop, "Price", 0.3) guiGridListAddColumn(gridlistVehicleShop, "ID", 0.3) guiSetVisible(windowVehicleShop,false) guiWindowSetSizable(windowVehicleShop, false) guiCreateStaticImage(53, 29, 223, 49, "images/vehicleshop.png", false, windowVehicleShop) buttonBuy = guiCreateButton(9, 498, 147, 29, "Buy", false, windowVehicleShop) buttonCancel = guiCreateButton(172, 498, 146, 29, "Cancel", false, windowVehicleShop) addEventHandler("onClientGUIClick",buttonBuy, createVehicleHandler, false) addEventHandler("onClientGUIClick",buttonCancel, closeVehicleShop, false) populateGridList( ) end addEventHandler("onClientResourceStart",getResourceRootElement(getThisResource()), function() createVehicleSelection() end ) local marker = createMarker ( 1671.2398681641, 1814.2731933594, 9.50, "cylinder", 1.5 ) setMarkerColor ( marker, 255, 255, 255, 255 ) function showVehicleSelection( hitElement ) if getElementType(hitElement) == "player" and (hitElement == localPlayer) then if not guiGetVisible(windowVehicleShop) then guiSetVisible(windowVehicleShop, true) showCursor(true) populateGridList( ) end end end addEventHandler( "onClientMarkerHit", marker, showVehicleSelection ) function closeVehicleShop () guiSetVisible ( windowVehicleShop, false ) showCursor( false ) end addEventHandler( "onClientGUIClick", buttonCancel, closeVehicleShop, false ) function populateGridList ( ) local file = xmlLoadFile ( "data/vehs.xml" ) if ( not file ) then assert ( file, "Fail to load vehs.xml" ) else local children = xmlNodeGetChildren ( file ) for _,child in ipairs ( children ) do local attrs = xmlNodeGetAttributes ( child ) local ID, Name, Price = attrs.id, attrs.name, attrs.price if ( not Name ) then outputDebugString ( "name attribute not set for item", 2 ) end if ( not Price ) then outputDebugString ( "cash attribute not set for item", 2 ) end if ( not ID ) then outputDebugString ( "score attribute not set for item", 2 ) end local itemRow = guiGridListAddRow ( gridlistVehicleShop ) guiGridListSetItemText ( gridlistVehicleShop, itemRow, 1, Name, false, false ) guiGridListSetItemText ( gridlistVehicleShop, itemRow, 2, Price, false, true ) guiGridListSetItemText ( gridlistVehicleShop, itemRow, 3, ID, false, true ) end xmlUnloadFile ( file ) end end function click ( button, state, sx, sy, x, y, z, elem, gui ) if ( ( state == "down" ) and ( source == gridlistVehicleShop ) ) then local ID = tonumber ( guiGridListGetItemText ( gridlistVehicleShop, guiGridListGetSelectedItem ( gridlistVehicleShop ), 1 ) ) if ( ID ) then triggerServerEvent ( "createVehicle", localPlayer, ID ) end end end addEventHandler( "onClientGUIClick", buttonBuy, click ) Server Side: function onResourceStart() createPed ( 17, 1671.2398681641, 1814.2731933594, 10.8203125, 90 ) createBlip ( 1671.2398681641, 1814.2731933594, 10.8203125, 55 ) end addEventHandler ( "onResourceStart", getRootElement(), onResourceStart ) function buyVehicle ( model ) createVehicle ( model, 1664.0642089844, 1809.1302490234, 10.8203125 ) end addEvent( "createVehicle", true ) addEventHandler( "createVehicle", root, buyVehicle )
myonlake Posted February 2, 2014 Posted February 2, 2014 There is no function for createVehicleHandler.
Waileer Posted February 2, 2014 Author Posted February 2, 2014 There is no function for createVehicleHandler. What should I use then?
al-Kobra Posted February 2, 2014 Posted February 2, 2014 function createVehicleSelection ( ) local sWidth, sHeight = guiGetScreenSize() local Width,Height = 328,537 local X = (sWidth/2) - (Width/2) local Y = (sHeight/2) - (Height/2) windowVehicleShop = guiCreateWindow(X,Y,Width,Height,"Vehicle Shop",false) gridlistVehicleShop = guiCreateGridList(9, 82, 308, 406, false, windowVehicleShop) guiGridListAddColumn(gridlistVehicleShop, "Name", 0.3) guiGridListAddColumn(gridlistVehicleShop, "Price", 0.3) guiGridListAddColumn(gridlistVehicleShop, "ID", 0.3) guiSetVisible(windowVehicleShop,false) guiWindowSetSizable(windowVehicleShop, false) guiCreateStaticImage(53, 29, 223, 49, "images/vehicleshop.png", false, windowVehicleShop) buttonBuy = guiCreateButton(9, 498, 147, 29, "Buy", false, windowVehicleShop) buttonCancel = guiCreateButton(172, 498, 146, 29, "Cancel", false, windowVehicleShop) addEventHandler("onClientGUIClick",buttonBuy, createVehicleHandler, false) addEventHandler("onClientGUIClick",buttonCancel, closeVehicleShop, false) populateGridList( ) end addEventHandler("onClientResourceStart",getResourceRootElement(getThisResource()), function() createVehicleSelection() end ) local marker = createMarker ( 1671.2398681641, 1814.2731933594, 9.50, "cylinder", 1.5 ) setMarkerColor ( marker, 255, 255, 255, 255 ) function showVehicleSelection( hitElement ) if getElementType(hitElement) == "player" and (hitElement == localPlayer) then if not guiGetVisible(windowVehicleShop) then guiSetVisible(windowVehicleShop, true) showCursor(true) populateGridList( ) end end end addEventHandler( "onClientMarkerHit", marker, showVehicleSelection ) function closeVehicleShop () guiSetVisible ( windowVehicleShop, false ) showCursor( false ) end addEventHandler( "onClientGUIClick", buttonCancel, closeVehicleShop, false ) Vehicles = { {'Ford',10000,602}, -- {carName,Price,ID} } function populateGridList ( ) local file = xmlLoadFile ( "data/vehs.xml" ) if ( not file ) then assert ( file, "Fail to load vehs.xml" ) else local children = xmlNodeGetChildren ( file ) for _,child in ipairs ( children ) do local attrs = xmlNodeGetAttributes ( child ) local ID, Name, Price = attrs.id, attrs.name, attrs.price if ( not Name ) then outputDebugString ( "name attribute not set for item", 2 ) end if ( not Price ) then outputDebugString ( "cash attribute not set for item", 2 ) end if ( not ID ) then outputDebugString ( "score attribute not set for item", 2 ) end for k,v in ipairs(Vehicles) do local itemRow = guiGridListAddRow(gridlistVehicleShop) guiGridListSetItemText (gridlistVehicleShop,itemRow,1,v[1],false,false) guiGridListSetItemText (gridlistVehicleShop,itemRow,2,v[2],false,false) guiGridListSetItemData ( gridlistVehicleShop,itemRow,2,v[2]) guiGridListSetItemData ( gridlistVehicleShop,itemRow,1,v[3]) end end xmlUnloadFile ( file ) end end addEventHandler('onClientGUIClick',root, function() if source == buttonBuy then if guiGridListGetSelectedItem(gridlistVehicleShop) ~= -1 then local car = guiGridListGetItemData ( gridlistVehicleShop,guiGridListGetSelectedItem ( gridlistVehicleShop ), 1) local price = guiGridListGetItemData ( gridlistVehicleShop,guiGridListGetSelectedItem ( gridlistVehicleShop ), 2) triggerServerEvent('CreateCarForPlayer',localPlayer,car,price) elseif source == buttonCancel then guiSetVisible (windowVehicleShop, false) showCursor (false) end end end ) -- Server Side # local vehicle = {} addEvent('CreateCarForPlayer',true) addEventHandler('CreateCarForPlayer',root,function(model,price) if getPlayerMoney(source) >= tonumber(price) then if isElement(vehicle[source]) then destroyElement(vehicle[source]) vehicle[source] = nil end local x,y,z = getElementPosition(source) vehicle[source] = createVehicle(tonumber(model),x,y,z) warpPedIntoVehicle(source,vehicle[source]) takePlayerMoney(source,tonumber(price)) end end ) addEventHandler('onPlayerQuit',root,function() if isElement(vehicle[source]) then destroyElement(vehicle[source]) vehicle[source] = nil end end ) Try this
Waileer Posted February 3, 2014 Author Posted February 3, 2014 It's a bit different. All is good only the problem is it won't spawn vehicle after selecting it from gridlist and pressing button.
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