Jump to content

[Question]Sending data from edit (client to server)


kieran

Recommended Posts

Hello, I am in the process of making a bank script, trying to keep it as simple as possible, stuck with getting the amount from edit box and taking it away from the players element data.  Here is code.

Client

local ATM_Locations = {
	{1690.0927734375, -2334.8369140625, 13.546875}
}

local ATMS = {}


local window = {}
local screenW, screenH = guiGetScreenSize()
	
function DrawATM()
	if not(isElement(window)) then
   	guiSetInputEnabled(false)
	showCursor(true)
        window = guiCreateWindow((screenW - 239) / 2, (screenH - 198) / 2, 239, 198, "ATM", false)
        guiWindowSetSizable(window, false)
        
		Amount_lbl = guiCreateLabel(98, 31, 55, 16, "Amount", false, window)
        edit = guiCreateEdit(25, 47, 190, 25, "", false, window)
		rad_with = guiCreateRadioButton(45, 80, 75, 25, "Withdraw", false, window)
		rad_dep = guiCreateRadioButton(135, 80, 65, 25, "Deposit", false, window)
		Balance_lbl = guiCreateLabel(80, 127, 200, 17, "Balance: ", false, window)
        accept_btn = guiCreateButton(10, 160, 99, 24, "Accept", false, window)
        exit_btn = guiCreateButton(130, 160, 99, 24, "Exit", false, window)    
		
		
		setElementData (localPlayer, "Bank.Balance",(getElementData (localPlayer, "Bank.Balance")))
		BankBalance = getElementData( localPlayer, "Bank.Balance" )
		if not (BankBalance) then
		guiSetText ( Balance_lbl, "Balance: 0" )
		setElementData( localPlayer, "Bank.Balance", 0 )
		--guiSetFont(Iron, "sa-header")  
		elseif (BankBalance) then
		guiSetText ( Balance_lbl, "Balance: " ..BankBalance )
		end
		
		
		addEventHandler("onClientGUIClick",accept_btn,acceptATM)
		addEventHandler("onClientGUIClick",exit_btn,exitATM)
	end
end


function exitATM()
	if(button == "left" and state == "up") then
		if (source == exit_btn) then
			if (isElement(window)) then
				guiSetInputEnabled(false)
				guiSetVisible(window, false)
				destroyElement(window)
				showCursor(false)
			end
		end
	end
end


function acceptATM()
	if(button == "left" and state == "up") then
		if (source == accept_btn) then
			BankBalance = getElementData( localPlayer, "Bank.Balance" )
			if (BankBalance) then
				if(guiRadioButtonGetSelected(rad_with))then
					if ( tonumber(BankBalance) <= tonumber (0)) then 
						outputChatBox("Insufficent funds are in your account, please put some money in first.")
					elseif ( tonumber(BankBalance) >= tonumber (1)) then 
						if (tonumber( BankBalance ) >= tonumber( amount )) then 
							amount = guiGetText(edit)
							setElementData (localPlayer, "Bank.Balance",(getElementData (localPlayer, "Bank.Balance")or 0 ) - amount)
							triggerServerEvent("withdraw", getRootElement(), localPlayer, amount)
						end
					end
				end
				if (guiRadioButtonGetSelected(rad_dep))then
					PlayerMoney = getPlayerMoney(localPlayer)
						if ( tonumber(PlayerMoney) <= tonumber (0)) then 
							outputChatBox("You must have at least 1 dollar... Go work!")
						elseif ( tonumber(PlayerMoney) >= tonumber (1)) then 
						if (tonumber( PlayerMoney ) >= tonumber( amount )) then 
							amount = guiGetText(edit)
							setElementData (localPlayer, "Bank.Balance",(getElementData (localPlayer, "Bank.Balance")or 0 ) + amount)
							triggerServerEvent("deposit", getRootElement(), localPlayer, amount)
						end
					end
				end
			end
		end
	end
end


function createGUI()
    for i=1,#ATM_Locations do 
        local x,y,z = ATM_Locations[i][1],ATM_Locations[i][2],ATM_Locations[i][3]
        ATMS[i] = createColTube( x,y,z,1,2 )
        addEventHandler( "onClientColShapeHit",ATMS[i], DrawATM )
    end
end

createGUI()

Server

function withdrawHandler(thePlayer, givenAmount)
   	if isElement(thePlayer) then
	givePlayerMoney(thePlayer, givenAmount)
  	end
end

addEvent("withdraw", true)
addEventHandler("withdraw", getRootElement(), withdrawHandler)


function depositHandler(thePlayer, takenAmount)
   	if isElement(thePlayer) then
	money = getPlayerMoney(thePlayer)
	setPlayerMoney(thePlayer, money - takenAmount)
  	end
end

addEvent("deposit", true)
addEventHandler("deposit", getRootElement(), depositHandler)

The GUI shows, both buttons don't work, the exit button worked before I tried adding "acceptATM" function, so the problem lies somewhere there, but nothing on debugscript 3

Thanks all for help :)

Edited by kieran
Link to comment

Solved one problem have another.

I made the rookie mistake of not adding button and state to function, hah! 

And also I forgot to add server script in meta :/ well at least this post will make others with similar problem double check there script xD

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