Chronic Posted October 22, 2013 Share Posted October 22, 2013 Hi I'm new to MTA and Lua, as you can probably tell by now. After being introduced to it by an experienced Lua scripter, I tried to make my own script, but failed. [17:31:36] WARNING: ammunation\server.lua:19: Bad argument @ 'addEventHandler Expected element at argument 2, got nil] Server function marker () ped = createPed (200, 0, -2, 5) theMarker = createMarker ( 0, 0, 3, "cylinder", 1, 0, 255, 0, 170 ) end addCommandHandler ("x", marker) function takeCash ( thePlayer, command, amount) takePlayerMoney ( thePlayer, 100 ) giveWeapon ( thePlayer , 31, 200 ) end function consoleGive ( thePlayer, commandName, weaponID, ammo ) giveWeapon (thePlayer, 31, 200, true ) end function gui () triggerClientEvent ( "onAmmunation", getRootElement(), ammunationHandler ) end addEventHandler ( "onMarkerHit", theMarker, gui ) Client function ammunationHandler () sx,sy,sz = guiGetScreenSize window[1] = guiCreateWindow(sx/2, sy/2, 800/2, 300/2, "Buy Weapon", false) weapon[1] = guiCreateButton(606, 316, 241, 83, "Buy Weapons", false, window[1]) end addEvent ( "onAmmunation", true ) addEventHandler ( "onAmmunation", getRootElement(), ammunationHandler ) Link to comment
Castillo Posted October 22, 2013 Share Posted October 22, 2013 That's because you're adding the event handler before the marker is created, move: addEventHandler ( "onMarkerHit", theMarker, gui ) to "marker" function, after createMarker. Link to comment
Chronic Posted October 22, 2013 Author Share Posted October 22, 2013 Thanks, that got rid of the error, but the gui still doesn't open. Any advice? So now I see I added a sz, which doesn't make sense because the screen doesn't have depth, so I got rid of it. After looking at the debug it says "attempted to perform arithmetic on global 'sx' (a function value) Link to comment
Castillo Posted October 22, 2013 Share Posted October 22, 2013 sx,sy,sz = guiGetScreenSize You forgot the parentheses, and 'sz' is not doing anything there, guiGetScreenSize returns two arguments only. sx, sy = guiGetScreenSize ( ) Link to comment
Chronic Posted October 22, 2013 Author Share Posted October 22, 2013 (edited) Thanks man, fixed my problem. EDIT: After some scripting, I made the GUI show up but the button does not. Client: function ammunationHandler () showCursor(true) toggleAllControls(true) sx,sy = guiGetScreenSize () window = guiCreateWindow(sx/2-648/2, sy/2-303/2, 648, 303, "Buy Weapon", false) guiWindowSetMovable(window, true) guiWindowSetSizable(window, true) weapon = guiCreateButton(sx/2, sy/2, 400/2, 150/2, "Buy Weapons", false, window) addEventHandler ( "onClientGUIClick", weapon, takeCash, false ) end addEvent ( "onAmmunation", true ) addEventHandler ( "onAmmunation", getRootElement(), ammunationHandler ) function takeCash ( button ) if button == "left" then takePlayerMoney ( thePlayer, 100 ) giveWeapon ( thePlayer , 31, 200 ) end end Server: function marker () ped = createPed (200, 0, -2, 5) theMarker = createMarker ( 0, 0, 3, "cylinder", 1, 0, 255, 0, 170 ) addEventHandler ( "onMarkerHit", theMarker, gui ) end addEventHandler ( "onResourceStart", getRootElement(), marker ) function gui () triggerClientEvent ( "onAmmunation", getRootElement(), ammunationHandler ) end Edited October 23, 2013 by Guest Link to comment
Dealman Posted October 23, 2013 Share Posted October 23, 2013 Personally, I'd recommend you to write your code differently as it will be easier for yourself to understand the code if you are inexperienced. I too am pretty new to coding, but this is how I construct my code; Client Code; local sX, sY = guiGetScreenSize() mainWindow = guiCreateWindow(sx/2-648/2, sY/2-303/2, 648, 303, "Buy buyButton", false) guiWindowSetMovable(mainWindow, true) guiWindowSetSizable(mainWindow, true) guiSetVisible(mainWindow, false) buyButton = guiCreateButton(sX/2, sY/2, 400/2, 150/2, "Buy buyButtons", false, mainWindow) guiSetVisible(buyButton, false) function ammunationHandler() if(guiGetVisible(mainWindow) == false) then showCursor(true) toggleAllControls(true) guiSetVisible(mainWindow, true) guiSetVisible(buyButton, true) end end addEvent("onAmmunation", true) addEventHandler("onAmmunation", getRootElement(), ammunationHandler) function takeCash() if(source == buyButton) then takePlayerMoney(localPlayer, 100) giveWeapon(localPlayer , 31, 200) end end addEventHandler("onClientGUIClick", getRootElement(), takeCash) Server Code; function createMarkerOnStart() thePed = createPed(200, 0, -2, 5) theMarker = createMarker(0, 0, 3, "cylinder", 1, 0, 255, 0, 170) end addEventHandler("onResourceStart", getRootElement(), createMarkerOnStart) function onPlayerHitMarker(hitElement) if(source == theMarker) then -- If the hit marker is "theMarker" then continue, else do nothing. if(getElementType(hitElement) == "player") then -- Make sure the element in the marker is a player. If not, do nothing. triggerClientEvent(hitElement, "onAmmunation", getRootElement(), ammunationHandler) -- Make sure it is sent to the player that hit the marker - and not everyone in the server. end end end addEventHandler("onMarkerHit", getRootElement(), onPlayerHitMarker) And remember, in the end, people write code differently as a matter of personal preference. Maybe you prefer it the way I do it, or maybe you don't. Either way, I really enjoy working with interface related code. Should you need any further assistance, feel free to PM me Link to comment
Chronic Posted October 23, 2013 Author Share Posted October 23, 2013 Yeah I have had a lot of people tell me I'm unorganized thanks for the advice, didn't fix my problem, but I figured it out on my own. Really careless of me to not see this Problem was the coordinates sx,sy = guiGetScreenSize () window = guiCreateWindow(sx/2-648/2, sy/2-303/2, 648, 303, "Buy Weapon", false) guiWindowSetMovable(window, true) guiWindowSetSizable(window, true) weapon = guiCreateButton(sx/2, sy/2, 400/2, 150/2, "Buy Weapons", false, window) Should be sx,sy = guiGetScreenSize () window = guiCreateWindow(sx/2-648/2, sy/2-303/2, 648, 303, "Buy Weapon", false) guiWindowSetMovable(window, true) guiWindowSetSizable(window, true) weapon = guiCreateButton(sx/2-648/2, sy/2-303/2, 374, 151, "Buy Weapons", false, window) Thanks to everyone that helped, much appreciated I will be sure to PM you if I have trouble Link to comment
Chronic Posted October 23, 2013 Author Share Posted October 23, 2013 I've been having problems with triggerServerEvent and triggerClientEvent, which is what I think the source of the problem is. When I do this Server Script, it works function marker () ped = createPed (200, 0, -2, 5) theMarker = createMarker ( 0, 0, 3, "cylinder", 1, 0, 255, 0, 170 ) addEventHandler ( "onMarkerHit", theMarker, takeCash2 ) end addCommandHandler ("x", marker ) function takeCash2 ( player, command, amount ) takeCash = takePlayerMoney ( player, 100 ) giveWep = giveWeapon ( player, 31, 200, true ) end Then when I do this: Server function marker () ped = createPed (200, 0, -2, 5) theMarker = createMarker ( 0, 0, 3, "cylinder", 1, 0, 255, 0, 170 ) addEventHandler ( "onMarkerHit", theMarker, gui ) end addEventHandler ( "onResourceStart", getRootElement(), marker ) function gui () triggerClientEvent ( "onammunationHandler", getRootElement(), ammunationHandler ) end function takeCash2 ( player, command, amount ) takeCash = takePlayerMoney ( player, 100 ) giveWep = giveWeapon ( player, 31, 200, true ) end addEvent ( "ontakeCash2", true ) addEventHandler ( "ontakeCash2", getRootElement(), takeCash2 ) Client function ammunationHandler () showCursor(true) toggleAllControls(true) sx,sy = guiGetScreenSize () window = guiCreateWindow(sx/2-648/2, sy/2-303/2, 648, 303, "Buy Weapon", false) guiWindowSetMovable(window, true) guiWindowSetSizable(window, true) weapon = guiCreateButton(sx/2-648/2, sy/2-303/2, 372, 151, "Buy Weapons", false, window) addEventHandler ( "onClientGUIClick", weapon, takeCashHandler, true ) end addEvent ( "onammunationHandler", true ) addEventHandler ( "onammunationHandler", getRootElement(), ammunationHandler ) function takeCashHandler ( ) triggerServerEvent ( "ontakeCash2", getRootElement(), takeCash2 ) end It doesn't work. I think it's probably because of the trigger events, either I don't need to use it, or I'm using it wrong. Both come up as Bad Argument in the debug. Any help would be amazing at this point, as I'm very close to finishing it. Link to comment
Dealman Posted October 23, 2013 Share Posted October 23, 2013 Since you just want to trigger an event, and not actually transfer any data - any optional argument isn't required. triggerServerEvent("ontakeCash2", localPlayer) The same goes for triggerClientEvent, since the server has no idea what ammunationHandler actually is, it will try to transfer nil. What is defined client-side is not defined server-side automatically. Also, what you are trying to transfer is the name of your functions - the optional arguments are meant for transferring data - for example; Client; local sendThis = "7" function exampleFunction() triggerServerEvent("onClientPlayerGreetWorld", localPlayer, sendThis) end addCommandHandler("HelloWorld", exampleFunction, false) -- False indicates that the command is NOT case-sensitive. Server; function anotherExampleFunction(receivedData) local triggeringPlayer = getPlayerName(source) outputChatBox(triggeringPlayer.."#FFFFFF has greeted the world! Data: #CC0000"..receivedData, getRootElement(), 255, 255, 255, true) end addEvent("onClientPlayerGreetWorld", true) addEventHandler("onClientPlayerGreetWorld", getRootElement(), anotherExampleFunction) As you can see, in client-side I have a variable with the string "7". I am able to transfer this to the server via using triggerServerEvent and adding the variable as an optional argument. You then have to fetch it with the server-side function, in this particular example I decided to name it "receivedData" but you could name it whatever you want. Maybe this will help you get a basic understanding of how it works. I'd also still recommend you to construct your code a bit differently, as it is hard to understand what exactly it is you're trying to do - and quite frankly it is a mess. If you write it similarly to how I demonstrated in one of my earlier replies - I believe you yourself too would be able to understand it easier. Link to comment
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