kieran Posted August 15, 2017 Share Posted August 15, 2017 (edited) I am in the middle of making a job script, it sets element data and I know how to get the data as there is a label that displays the players "iron". But I am trying to make a shop to sell the iron and I don't know how to get element data from a player that clicks a button. Client Spoiler function IronShop() SellIronWindow = {} local screenW, screenH = guiGetScreenSize() SellIronWindow = guiCreateWindow((screenW - 325) / 2, (screenH - 224) / 2, 325, 224, "Iron Shop", false) guiWindowSetSizable(SellIronWindow, false) AmountIronSelling = guiCreateEdit(74, 91, 177, 26, "", false, SellIronWindow) IronSellInfo1 = guiCreateLabel(74, 36, 172, 15, "You will recieve money for iron. ", false, SellIronWindow) IronSellInfo2 = guiCreateLabel(39, 56, 247, 17, "Enter the amount to sell into the box below.", false, SellIronWindow) button_sell_iron = guiCreateButton(74, 162, 79, 27, "Sell Iron", false, SellIronWindow) button_exit_iron = guiCreateButton(169, 162, 79, 27, "Exit", false, SellIronWindow) you_recieve_iron = guiCreateLabel(88, 132, 150, 15, "", false, SellIronWindow) setElementData (source, "Iron.pres",(getElementData (source, "Iron.pres"))) TheIron = getElementData( source, "Iron.pres" ) if (TheIron) then guiSetText ( you_recieve_iron, "Your current iron is: " ..TheIron ) addEventHandler("onClientGUIClick", button_sell_iron, SellMyIron) end end addEvent( "SellIron", true ) addEventHandler( "SellIron", root, IronShop) function SellMyIron(button,state) if(button == "left" and state == "up") then if (source == button_sell_iron) then setElementData (source, "Iron.pres",(getElementData (source, "Iron.pres"))) TheIron = getElementData( source, "Iron.pres" ) if (TheIron) then selling_iron = guiGetText(AmountIronSelling) if ( TheIron ) >= ( selling_iron ) then setElementData (source, "Iron.pres",(getElementData (hitElement, "Iron.pres")or 0 ) - selling_iron) money_for_iron = givePlayerMoney ( selling_iron*100 ) guiSetText ( you_recieve_iron, "Your recieved: " ..money_for_iron ) --outputChatBox (""..selling_iron) end end end end end Server Spoiler IronSellMark = createMarker (1144.701171875, 1977.615234375, 9.8203125, "cylinder", 1.5, 0, 200, 55, 255) IronSellPed = createPed (171, 1144.701171875, 1978.970703125, 10.8203125, 180) function IronSales ( hitElement, matchingDimension ) if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then if isPedOnGround ( hitElement ) then triggerClientEvent ( hitElement, "SellIron", hitElement ) end end end addEventHandler("onMarkerHit", IronSellMark, IronSales) Basically I want to get element data, then if it's greater or equal to the number in the edit box it will give the player money, the money will be the amount in edit box * 100 and then it will take the amount from edit box away from element data. Edited August 15, 2017 by kieran Link to comment
NeXuS™ Posted August 15, 2017 Share Posted August 15, 2017 Use localPlayer instead of hitElement. Link to comment
kieran Posted August 15, 2017 Author Share Posted August 15, 2017 3 minutes ago, NeXuS™ said: Use localPlayer instead of hitElement. It sort of works, but now it says "attempt to compare string with number" setElementData (localPlayer, "Iron.pres",(getElementData (localPlayer, "Iron.pres"))) TheIron = getElementData( localPlayer, "Iron.pres" ) if (TheIron) then selling_iron = guiGetText(AmountIronSelling) if ( TheIron ) >= ( selling_iron ) then Do I need to convert string into number? How can I do that? Link to comment
kieran Posted August 15, 2017 Author Share Posted August 15, 2017 (edited) 15 minutes ago, NeXuS™ said: Use tonumber. Thanks, works a treat... But I have one final issue it is now saying "attempt to concatenate global 'money_for_iron' (a boolean value) function SellMyIron(button,state) if(button == "left" and state == "up") then if (source == button_sell_iron) then setElementData (localPlayer, "Iron.pres",(getElementData (localPlayer, "Iron.pres"))) TheIron = getElementData( localPlayer, "Iron.pres" ) if (TheIron) then selling_iron = guiGetText(AmountIronSelling) if (tonumber( TheIron ) >= tonumber( selling_iron )) then setElementData (localPlayer, "Iron.pres",(getElementData (localPlayer, "Iron.pres")or 0 ) - selling_iron) money_for_iron = givePlayerMoney ( selling_iron*100 ) guiSetText ( you_recieve_iron, "Your recieved: " ..money_for_iron ) --outputChatBox (""..selling_iron) end end end end end Not really an important part, but would be nice to have in it. says problem is on line 11 (on above snippet) Edited August 15, 2017 by kieran Link to comment
Tails Posted August 15, 2017 Share Posted August 15, 2017 givePlayerMoney, Returns true if the money was added, or false if invalid parameters were passed. According to wiki, so not the amount. If you still want to output it, use tostring(money_for_iron) 1 Link to comment
kieran Posted August 15, 2017 Author Share Posted August 15, 2017 (edited) 25 minutes ago, Tails said: givePlayerMoney, Returns true if the money was added, or false if invalid parameters were passed. According to wiki, so not the amount. If you still want to output it, use tostring(money_for_iron) It just returns true, how can I get it to return amount? money_for_iron = givePlayerMoney ( selling_iron*100 ) recieved_money_for_iron = (tostring (money_for_iron)) guiSetText ( you_recieve_iron, "You recieved: $" ..recieved_money_for_iron ) Edited August 15, 2017 by kieran Link to comment
NeXuS™ Posted August 15, 2017 Share Posted August 15, 2017 recieved_money_for_iron = selling_iron*100 Link to comment
kieran Posted August 15, 2017 Author Share Posted August 15, 2017 3 hours ago, NeXuS™ said: recieved_money_for_iron = selling_iron*100 Thanks, works great.... But now there's the small problem of keeping clients cash when server goes off... Here is what I tried. Server Spoiler function SaveClientCash(quitType, reason, responsibleElement) if not (isGuestAccount (getPlayerAccount (source))) then local account = getPlayerAccount (source) if (account) then local MyIronMoneyz = getElementData( source, "moneyz.set" ) if (MyIronMoneyz) then setAccountData (account, "moneyz.saved", (MyIronMoneyz (source))) end end end end addEventHandler("onPlayerQuit", getRootElement(), SaveClientCash) function LoadClientCash(quitType, reason, responsibleElement) -- i did that function to restore your iron when rejoin to server. if not (isGuestAccount (getPlayerAccount (source))) then local account = getPlayerAccount (source) if (account) then local MyIronMoneyz = getAccountData( account, "moneyz.saved" ) if (MyIronMoneyz) then setPlayerMoney (source, MyIronMoneyz) else setPlayerMoney (source, 200) end end end end addEventHandler("onPlayerLogin", getRootElement(), LoadClientCash) Client Spoiler function SellMyIron(button,state) if(button == "left" and state == "up") then if (source == button_sell_iron) then setElementData (localPlayer, "Iron.pres",(getElementData (localPlayer, "Iron.pres"))) TheIron = getElementData( localPlayer, "Iron.pres" ) if (TheIron) then selling_iron = guiGetText(AmountIronSelling) if (tonumber( TheIron ) >= tonumber( selling_iron )) then setElementData (localPlayer, "Iron.pres",(getElementData (localPlayer, "Iron.pres")or 0 ) - selling_iron) money_for_iron = givePlayerMoney ( selling_iron*100 ) MyIronMoney = getPlayerMoney ( localPlayer ) setElementData (localPlayer, "moneyz.set", MyIronMoney) recieved_money_for_iron = selling_iron*100 guiSetText ( you_recieve_iron, "You got " ..recieved_money_for_iron.. " dollars" ) else guiSetText ( you_recieve_iron, "Error, please check you have iron." ) end end end end end It shows "attempt to call 'MyIronMoneyz' (a number value)" Basically I am trying to set element data after getting money client side so I can get it server side and set the account data. I tried using toString but no joy, same with tonumber. MyIronMoney = getPlayerMoney (tonumber( localPlayer )) setElementData (localPlayer, "moneyz.set", MyIronMoney) It says that problem is server side on line 7... After I get this working that's it pretty much done, rest is just designing GUI etc, which is easy. Link to comment
NeXuS™ Posted August 15, 2017 Share Posted August 15, 2017 function SaveClientCash(quitType, reason, responsibleElement) if not (isGuestAccount (getPlayerAccount (source))) then local account = getPlayerAccount (source) if (account) then local MyIronMoneyz = getElementData( source, "moneyz.set" ) if (MyIronMoneyz) then setAccountData (account, "moneyz.saved", MyIronMoneyz) end end end end addEventHandler("onPlayerQuit", getRootElement(), SaveClientCash) Why did you try to use the "MyIronMoneyz" variable as a function? Link to comment
kieran Posted August 15, 2017 Author Share Posted August 15, 2017 6 minutes ago, NeXuS™ said: function SaveClientCash(quitType, reason, responsibleElement) if not (isGuestAccount (getPlayerAccount (source))) then local account = getPlayerAccount (source) if (account) then local MyIronMoneyz = getElementData( source, "moneyz.set" ) if (MyIronMoneyz) then setAccountData (account, "moneyz.saved", MyIronMoneyz) end end endendaddEventHandler("onPlayerQuit", getRootElement(), SaveClientCash) Why did you try to use the "MyIronMoneyz" variable as a function? Well I am pretty new to using element data to save to account data, my PC went down a few weeks ago and only got back to scripting last week... So forgot a lot. I am trying to get the data and then setAccountData to be the element data, which will be the players money in this case, I am trying to force the server to update cash from client. Without it updating from client to server it won't save, so this is only way I can think of as my GUI is client side. If this doesn't make sense try read it over a few times haha, I am bad at explaining it. Link to comment
NeXuS™ Posted August 15, 2017 Share Posted August 15, 2017 Giving money on client-side wont really work as you would expect. Link to comment
kieran Posted August 15, 2017 Author Share Posted August 15, 2017 (edited) 11 minutes ago, NeXuS™ said: Giving money on client-side wont really work as you would expect. Then how could I save it? The whole GUI system is meant to take x amount away from the players "iron" and add the x amount of money to his account... Should I just try setting the money as data (save it as a number) then trigger an event server side to get the number and give the player that amount of money? There must be a way to set it client side, otherwise givePlayerMoney should just be server function, right? Edited August 15, 2017 by kieran Link to comment
NeXuS™ Posted August 15, 2017 Share Posted August 15, 2017 givePlayerMoney on client-side does give money to the player, but only on his side. Use a custom event and trigger it with the player and the amount of the money as an arg. 1 Link to comment
kieran Posted August 15, 2017 Author Share Posted August 15, 2017 To be honest that's making me a little more confused.... How would I make the argument? Link to comment
NeXuS™ Posted August 15, 2017 Share Posted August 15, 2017 Server-side function customEventHandler(thePlayer, givenAmount) if isElement(thePlayer) then givePlayerMoney(thePlayer, givenAmount) end end addEvent("customEvent", true) addEventHandleR("customEvent", getRootElement(), customEventHandler) Client side triggerServerEvent("customEvent", getRootElement(), localPlayer, ironMoney) Just supposed to work. Link to comment
kieran Posted August 15, 2017 Author Share Posted August 15, 2017 How do I get the amount? Would I just change money_for_iron = givePlayerMoney ( selling_iron*100 ) to ironMoney = ( selling_iron*100 ) and then use your event? Link to comment
kieran Posted August 15, 2017 Author Share Posted August 15, 2017 Thanks so much for all your help guys, works amazingly now, learned a lot! 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