Jump to content

Checking money


dima3367

Recommended Posts

rentCar = {}

function rentCarFuntion ( player ) 
    if getDistanceBetweenPoints3D ( -1981.5, 128.5, 27.6875, getElementPosition(player) ) <2 then 
       rentCar[player] = createVehicle( 462, -1982.959, 122.84863, 27.6875, 0, 0, 90, "Rent" ) 
       warpPedIntoVehicle( player, rentCar[player] )
       takePlayerMoney(player, 25)
	   outputChatBox ( "You rent car!", getRootElement(), 0, 255, 0, true )
       setTimer( 
       function ()
          destroyElement(rentCar[player])
		   outputChatBox ( "Transport's rent over", getRootElement(), 255, 0, 0, true )
       end
       ,600000,1 
       )
    end
end 
addEvent( "rentroller", true )
addEventHandler( "rentroller", getRootElement(),rentCarFuntion )
addCommandHandler("rentroller",rentCarFuntion ) 

 

Hello. I have a script transport rentals. Please tell me how to do a background check on the availability of money from the player? Who can please an example in the above code with comments, thank you in advance!

Link to comment
function rentCarFuntion ( player ) 
    if getDistanceBetweenPoints3D ( -1981.5, 128.5, 27.6875, getElementPosition(player) ) <2 and getPlayerMoney >=25 then 
       rentCar[player] = createVehicle( 462, -1982.959, 122.84863, 27.6875, 0, 0, 90, "Rent" ) 
       warpPedIntoVehicle( player, rentCar[player] )
       takePlayerMoney(player, 25)
	   outputChatBox ( "You rent car!", getRootElement(), 0, 255, 0, true )
       setTimer( 
       function ()
          destroyElement(rentCar[player])
		   outputChatBox ( "Transport's rent over", getRootElement(), 255, 0, 0, true )
       end
       ,600000,1 
       )
    end
end 
addEvent( "rentroller", true )
addEventHandler( "rentroller", getRootElement(),rentCarFuntion )
addCommandHandler("rentroller",rentCarFuntion ) 

right?
if I want to make a notification to a player that he doesn't have enough money to chat, I must, after line 15 insert function outputChatBox? 
or how?

Link to comment

If the player is near enough to the point AND he has the money the code from and including line 3 to line 13 will be executed. In fact the end at line 14 closes the if at line 2.

The code after the end will be executed regardeless if the above code gets executed. To make the notification appear only if the above code got executed you have to use the "else" statement, as you may know

local someCondition = true

if someCondition then
	--code gets executed if someCondition is true
else
	--this code will be executed if someCondition is NOT a true-value (i.e. false or nil)
end

BUT

If the player is not near enough the code block will also be skipped. So we must split the split (or, not recommended, recheck the value)

function rentCarFuntion ( player )
	if getDistanceBetweenPoints3D ( -1981.5, 128.5, 27.6875, getElementPosition(player) ) < 2 then
		--the player is near
		if getPlayerMoney >=25 then
			--he has the money
			rentCar[player] = createVehicle( 462, -1982.959, 122.84863, 27.6875, 0, 0, 90, "Rent" )
			warpPedIntoVehicle( player, rentCar[player] )
			takePlayerMoney(player, 25)
			outputChatBox ( "You rent car!", getRootElement(), 0, 255, 0, true )
			setTimer( 
				function ()
					destroyElement(rentCar[player])
					outputChatBox ( "Transport's rent over", getRootElement(), 255, 0, 0, true )
				end,
			600000, 1 )
		else
			--he does not have the money
			outputChatBox( "Not enough money! Cost is: 25", root, 255, 0, 0, true )
		end
	end
end
addEvent( "rentroller", true )
addEventHandler( "rentroller", getRootElement(),rentCarFuntion )
addCommandHandler("rentroller",rentCarFuntion ) 

 

  • Like 1
Link to comment

Hello again. Why does not want to run the script.
He always says "Not enough money Cost is:! 25", although enough money that can be?
Perhaps my client of the need to finish that?

function guiClick()
    if (source == button1) then
        local kunde = getLocalPlayer()
		triggerServerEvent ( "rentroller", getLocalPlayer(), kunde )
		guiSetInputEnabled ( false )
		guiSetVisible ( Window, false )
		showCursor ( false )
	end
end
addEventHandler("onClientGUIClick", getRootElement(), guiClick)

 

Link to comment

No no the problem is that i'm an idiot 

if getPlayerMoney >=25 then 

i'm comparing a function with a number...

replace line 4 with

if getPlayerMoney(player) >=25 then

 

tell me if it's working after this edit

 

PS:  Warning: You should use the global variable client serverside instead of passing the localPlayer by parameter or source. Otherwise event faking (passing another player instead of the localPlayer) would be possible. More information at addEventHandler

insert between line 1 and 2 this:

player = client or player

That way if the function is used as handler for the event, "client" will be the player that triggered the event and the variable "player" will have that player as value without security problems

If the function is called from the commandhandler, "client" will be nil and so the "player" variable will have as value the player that typed the command "rentroller"

 

Edited by LoPollo
  • Like 1
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...