Jump to content

[Question] Getting player that clicked button


kieran

Recommended Posts

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

Edited by kieran
Link to comment
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
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. :D  says problem is on line 11 (on above snippet)

Edited by kieran
Link to comment
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 by kieran
Link to comment
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. :P  

Link to comment
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
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
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 by kieran
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...