Jump to content

Why not working buyWeapon?


Recommended Posts

function buyWeapon(thePlayer, command)
	local mycoins = exports.coinsystem:getPlayerCoin(thePlayer)
		if (mycoins >= 1) then
			giveWeapon(thePlayer, 31, 2000)
			exports.coinsystem:takePlayerCoin(thePlayer, prize)
			outputChatBox("you bought a M4.", thePlayer, 0, 255, 0, false)
		else
			outputChatBox("you don't have enough coins!", thePlayer, 255, 0, 0, false)
	end
end
addCommandHandler("buyw", buyWeapon)

i got this error: xy.lua:17: attempt to compare number with nil

Link to comment
4 minutes ago, *BeaT* said:

Please post the full script because, for example, prize is not declared

Second, you don't have line 17 in your posted script

Please tell us what the command is supposed to do( how it should work )

This is the full code, sorry, line 3

Link to comment

Uou get this error because the

Quote

exports.coinsystem:getPlayerCoin(thePlayer)

Export, returns nil. So you can try this:

function buyWeapon( player, command )
	local p_coins = exports.coinsystem:getPlayerCoin( player )
	if tonumber( p_coins ) >= 1 then
		giveWeapon(player, 31, 2000)
		exports.coinsystem:takePlayerCoin(player, prize)
		outputChatBox("you bought a M4.", player, 0, 255, 0, false)		
	else
		outputChatBox("you don't have enough coins!", player, 255, 0, 0, false)
	end
end

 

  • Like 1
Link to comment
9 minutes ago, aka Blue said:

Uou get this error because the

Export, returns nil. So you can try this:


function buyWeapon( player, command )
	local p_coins = exports.coinsystem:getPlayerCoin( player )
	if tonumber( p_coins ) >= 1 then
		giveWeapon(player, 31, 2000)
		exports.coinsystem:takePlayerCoin(player, prize)
		outputChatBox("you bought a M4.", player, 0, 255, 0, false)		
	else
		outputChatBox("you don't have enough coins!", player, 255, 0, 0, false)
	end
end

 

working with command, but with button, doesn't working, why? :S 

addEvent( "m4", true ) 
addEventHandler( "m4", root, 
function ()
	local p_coins = exports.coinsystem:getPlayerCoin(player)
	if tonumber( p_coins ) >= 400 then
		giveWeapon(player, 31, 2000)
		exports.coinsystem:takePlayerCoin(player, 400)
		outputChatBox("you bought a M4.", player, 0, 255, 0, false)		
	else
		outputChatBox("you don't have enough coins!", player, 255, 0, 0, false)
	end
end
)

i got this error: xy.lua:3: attempt to compare number with nil

Link to comment

Because you use a player argument but its not defined. ¿Understand?. When you trigger to server, take sure you send the getLocalPlayer in the server too, something like this, see:

-- Client

triggerServerEvent( "m4", resourceRoot, getLocalPlayer( ) )

-- Server

addEvent( "m4", true ) 
addEventHandler( "m4", root, 
function ( player )
	local p_coins = exports.coinsystem:getPlayerCoin(player)
	if tonumber( p_coins ) >= 400 then
		giveWeapon(player, 31, 2000)
		exports.coinsystem:takePlayerCoin(player, 400)
		outputChatBox("you bought a M4.", player, 0, 255, 0, false)		
	else
		outputChatBox("you don't have enough coins!", player, 255, 0, 0, false)
	end
end
)

 

  • Like 1
Link to comment
1 minute ago, aka Blue said:

Because you use a player argument but its not defined. ¿Understand?. When you trigger to server, take sure you send the getLocalPlayer in the server too, something like this, see:


-- Client

triggerServerEvent( "m4", resourceRoot, getLocalPlayer( ) )

-- Server

addEvent( "m4", true ) 
addEventHandler( "m4", root, 
function ( player )
	local p_coins = exports.coinsystem:getPlayerCoin(player)
	if tonumber( p_coins ) >= 400 then
		giveWeapon(player, 31, 2000)
		exports.coinsystem:takePlayerCoin(player, 400)
		outputChatBox("you bought a M4.", player, 0, 255, 0, false)		
	else
		outputChatBox("you don't have enough coins!", player, 255, 0, 0, false)
	end
end
)

 

thanks

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