Turbe$Z

What wrong in this simple pay script?

Recommended Posts

function payScript(player,cmd,other,amount) 
local money = getPlayerMoney(player) 
local otherPlayer = getPlayerFromName(other) 
	if not other or not amount then 
		outputChatBox("#00BAFF[Play] #FFffFFHelyes használat: #0088FF/pay <név> <összeg>!",player,0,255,255, true) 
	end 
			if ((money - amount) < 0) then 
				outputChatBox("#00BAFF[Play] #FFffFFNincs elegendő #0088FFpénzed!",player,0,255,255, true) 
				return 
			else 
				setPlayerMoney(otherPlayer,getPlayerMoney(otherPlayer) + amount) 
				setPlayerMoney(player,money - amount) 
				outputChatBox("#00bAFF[Play] #0088FF"..amount.."Ft-ot #FFffFFküldtél neki: #0088FF"..other.."!",player,0,255,255, true) 
				outputChatBox("#00BAFF[Play] #FFffFFKaptál #0088FF"..amount.."Ft-ot #FFffFFtőle: #0088FF"..getPlayerName(player).."!",otherPlayer,0,255,255,true) 
	end 
end 
addCommandHandler("pay",payScript) 

and i got this error:

xy.lua:11: attempt to perform arithmetic on a boolean value

how to fix this? :S

Share this post


Link to post

other is not a valid player name, so  local otherPlayer = getPlayerFromName(other) is false, so  getPlayerMoney(otherPlayer) + amount  is getPlayerMoney(false) + amount, which should also throw a warning.

Just checked really fast, so i'm sorry if i said something wrong.

Share this post


Link to post
7 minutes ago, LoPollo said:

other is not a valid player name, so  local otherPlayer = getPlayerFromName(other) is false, so  getPlayerMoney(otherPlayer) + amount  is getPlayerMoney(false) + amount, which should also throw a warning.

Just checked really fast, so i'm sorry if i said something wrong.

:S how to fix this?

Share this post


Link to post

You probably want to check the input before doing anything, so move the if from line 4 up to line 2.
Then in line 4 you should check if otherPlayer is not false...so something like this.

function payScript(player,cmd,other,amount) 
	if not other or not amount then 
		outputChatBox( "#00BAFF[Play] #FFffFFHelyes használat: #0088FF/pay <név> <összeg>!", player, 0, 255, 255, true ) 
	else
		local money = getPlayerMoney(player)
		local otherPlayer = getPlayerFromName(other) 
		if otherPlayer then
			if (money - amount) < 0 then 
				outputChatBox("#00BAFF[Play] #FFffFFNincs elegendo #0088FFpénzed!",player,0,255,255, true) 
			else 
				setPlayerMoney( otherPlayer, getPlayerMoney(otherPlayer) + amount ) 
				outputChatBox( "#00BAFF[Play] #FFffFFKaptál #0088FF"..amount.."Ft-ot #FFffFFtole: #0088FF".. getPlayerName(player).."!",otherPlayer,0,255,255,true )
				
				setPlayerMoney( player, money - amount )
				outputChatBox( "#00bAFF[Play] #0088FF"..amount.."Ft-ot #FFffFFküldtél neki: #0088FF"..other.."!",player,0,255,255, true )
			end
		else
			outputChatBox( "Could not find player ".. other, player, 0, 255, 255, true )
		end
	end
end
addCommandHandler( "pay", payScript ) 

 

  • Like 1

Share this post


Link to post
38 minutes ago, 3aGl3 said:

You probably want to check the input before doing anything, so move the if from line 4 up to line 2.
Then in line 4 you should check if otherPlayer is not false...so something like this.


function payScript(player,cmd,other,amount) 
	if not other or not amount then 
		outputChatBox( "#00BAFF[Play] #FFffFFHelyes használat: #0088FF/pay <név> <összeg>!", player, 0, 255, 255, true ) 
	else
		local money = getPlayerMoney(player)
		local otherPlayer = getPlayerFromName(other) 
		if otherPlayer then
			if (money - amount) < 0 then 
				outputChatBox("#00BAFF[Play] #FFffFFNincs elegendo #0088FFpénzed!",player,0,255,255, true) 
			else 
				setPlayerMoney( otherPlayer, getPlayerMoney(otherPlayer) + amount ) 
				outputChatBox( "#00BAFF[Play] #FFffFFKaptál #0088FF"..amount.."Ft-ot #FFffFFtole: #0088FF".. getPlayerName(player).."!",otherPlayer,0,255,255,true )
				
				setPlayerMoney( player, money - amount )
				outputChatBox( "#00bAFF[Play] #0088FF"..amount.."Ft-ot #FFffFFküldtél neki: #0088FF"..other.."!",player,0,255,255, true )
			end
		else
			outputChatBox( "Could not find player ".. other, player, 0, 255, 255, true )
		end
	end
end
addCommandHandler( "pay", payScript ) 

 

Thanks  :DDD But players why can give minus value?

Share this post


Link to post

Because there is no check for that, just add a amount > 0 to the first if.

Share this post


Link to post
5 minutes ago, 3aGl3 said:

Because there is no check for that, just add a amount > 0 to the first if.

i added to first if, and i get this error: attempt to compare number with string :/ 

Share this post


Link to post

function getPlayerFromPartialName(name)
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil
    if name then
        for _, player in ipairs(getElementsByType("player")) do
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower()
            if name_:find(name, 1, true) then
                return player
            end
        end
    end
end

function payScript(player,cmd,other,amount) 
    if not other or not amount then 
        outputChatBox( "#00BAFF[Play] #FFffFFHelyes használat: #0088FF/pay <név> <összeg>!", player, 0, 255, 255, true ) 
    else
        if tonumber(amount) < 0  then
        outputChatBox( "amount is in negative", player, 0, 255, 255, true )     
        return     
        end
        local money = getPlayerMoney(getPlayerFromPartialName(other))
        local otherPlayer = getPlayerFromPartialName(other)
        if otherPlayer and otherPlayer ~= player  then
            if (money - amount) < 0 then 
                outputChatBox("#00BAFF[Play] #FFffFFNincs elegendo #0088FFpénzed!",player,0,255,255, true) 
            else 
                setPlayerMoney( otherPlayer, getPlayerMoney(otherPlayer) + amount ) 
                outputChatBox( "#00BAFF[Play] #FFffFFKaptál #0088FF"..amount.."Ft-ot #FFffFFtole: #0088FF".. getPlayerName(player).."!",otherPlayer,0,255,255,true )
                
                setPlayerMoney( player, money - amount )
                outputChatBox( "#00bAFF[Play] #0088FF"..amount.."Ft-ot #FFffFFküldtél neki: #0088FF"..getPlayerName(otherPlayer).."!",player,0,255,255, true )
            end
        else
            outputChatBox( "Could not find player ".. other, player, 0, 255, 255, true )
        end
    end
end
addCommandHandler( "pay", payScript ) 

use this and change this line to your need 



outputChatBox( "amount is in negative", player, 0, 255, 255, true )  

 

Edited by Ayush Rathore
  • Like 1

Share this post


Link to post
3 hours ago, Ayush Rathore said:

function getPlayerFromPartialName(name)
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil
    if name then
        for _, player in ipairs(getElementsByType("player")) do
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower()
            if name_:find(name, 1, true) then
                return player
            end
        end
    end
end

function payScript(player,cmd,other,amount) 
    if not other or not amount then 
        outputChatBox( "#00BAFF[Play] #FFffFFHelyes használat: #0088FF/pay <név> <összeg>!", player, 0, 255, 255, true ) 
    else
        if tonumber(amount) < 0  then
        outputChatBox( "amount is in negative", player, 0, 255, 255, true )     
        return     
        end
        local money = getPlayerMoney(getPlayerFromPartialName(other))
        local otherPlayer = getPlayerFromPartialName(other)
        if otherPlayer and otherPlayer ~= player  then
            if (money - amount) < 0 then 
                outputChatBox("#00BAFF[Play] #FFffFFNincs elegendo #0088FFpénzed!",player,0,255,255, true) 
            else 
                setPlayerMoney( otherPlayer, getPlayerMoney(otherPlayer) + amount ) 
                outputChatBox( "#00BAFF[Play] #FFffFFKaptál #0088FF"..amount.."Ft-ot #FFffFFtole: #0088FF".. getPlayerName(player).."!",otherPlayer,0,255,255,true )
                
                setPlayerMoney( player, money - amount )
                outputChatBox( "#00bAFF[Play] #0088FF"..amount.."Ft-ot #FFffFFküldtél neki: #0088FF"..getPlayerName(otherPlayer).."!",player,0,255,255, true )
            end
        else
            outputChatBox( "Could not find player ".. other, player, 0, 255, 255, true )
        end
    end
end
addCommandHandler( "pay", payScript ) 

use this and change this line to your need 



 

outputChatBox( "amount is in negative", player, 0, 255, 255, true )  

 

working the minus value, but now the script, when i type for example (i have 99.999.999$) and i type /pay xyplayer 1 , everyone money has going to 64k$, then i type again, and the my money +1, and doesn't send the money to the other player. how to fix this bug? O.o:/ 

 

(sorry for my bad English:()

Share this post


Link to post

Try this


function getPlayerFromPartialName(name)
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil
    if name then
        for _, player in ipairs(getElementsByType("player")) do
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower()
            if name_:find(name, 1, true) then
                return player
            end
        end
        else
        return false
    end
end

function payScript(player,cmd,other,amount)     
   local name = getPlayerFromPartialName(name)
   local atg
   local rpm = getPlayerMoney(player)
   local err = {}
   
   if not (name and isElement(name)) then
   err[1] = "Name not specified correctly."
   end
   
   if tonumber(amount) < 0 then
   err[2] = "Amount can't be negative."
   end
 
   atg = tonumber(amount)
   
   if (rpm-atg) < 0 then
   err[2] = "Given amount is more than yours."
   end
   
   if #err == 0 then
   setPlayerMoney(name,atg)
   outputChatBox("Given:"..atg.." to:"..getPlayerName(name),player,0,255,0)
   setPlayerMoney(player,(rpm-atg))
   outputChatBox("Left:"..(rpm-atg).." to you.",player,0,255,0)
   else
   for i=1,#err do 
   outputChatBox(err[1],player,0,255,0)
   end
   end
   
end
addCommandHandler( "pay", payScript ) 

 

  • Like 2

Share this post


Link to post
function getPlayerFromPartialName(name)
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil
    if name then
        for _, player in ipairs(getElementsByType("player")) do
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower()
            if name_:find(name, 1, true) then
                return player
            end
        end
        else
        return false
    end
end

function payScript(player,cmd,other,amount)     
   local name = getPlayerFromPartialName(name)
   local atg
   local rpm = getPlayerMoney(player)
   local err = {}
   
   if not (name and isElement(name)) then
   err[#err+1] = "Name not specified correctly."
   end
   
   if tonumber(amount) < 0 then
   err[#err+1] = "Amount can't be negative."
   end
 
   atg = tonumber(amount)
   
   if (rpm-atg) < 0 then
   err[#err+1] = "Given amount is more than yours."
   end
   
   if #err == 0 then
   setPlayerMoney(name,atg)
   outputChatBox("Given:"..atg.." to:"..getPlayerName(name),player,0,255,0)
   setPlayerMoney(player,(rpm-atg))
   outputChatBox("Left:"..(rpm-atg).." to you.",player,0,255,0)
   else
   for i=1,#err do 
   outputChatBox(err[i],player,0,255,0)
   end
   end
   
end
addCommandHandler( "pay", payScript ) 

If any one has copied it please update it with this 

:)

Edited by Ayush Rathore
contains some error
  • Like 1

Share this post


Link to post
29 minutes ago, Ayush Rathore said:

function getPlayerFromPartialName(name)    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil    if name then        for _, player in ipairs(getElementsByType("player")) do            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower()            if name_:find(name, 1, true) then                return player            end        end        else        return false    endendfunction payScript(player,cmd,other,amount)        local name = getPlayerFromPartialName(name)   local atg   local rpm = getPlayerMoney(player)   local err = {}      if not (name and isElement(name)) then   err[#err+1] = "Name not specified correctly."   end      if tonumber(amount) < 0 then   err[#err+1] = "Amount can't be negative."   end    atg = tonumber(amount)      if (rpm-atg) < 0 then   err[#err+1] = "Given amount is more than yours."   end      if #err == 0 then   setPlayerMoney(name,atg)   outputChatBox("Given:"..atg.." to:"..getPlayerName(name),player,0,255,0)   setPlayerMoney(player,(rpm-atg))   outputChatBox("Left:"..(rpm-atg).." to you.",player,0,255,0)   else   for i=1,#err do    outputChatBox(err[i],player,0,255,0)   end   end   endaddCommandHandler( "pay", payScript ) 

If any one has copied it please update it with this 

:)

doesn't working :( i type a online player's name, but the script says "Name not specified correctly" :( 

Share this post


Link to post

wait i will correct it ang give it to you

 

function getPlayerFromPartialName(name)
    local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil
    if name then
        for _, player in ipairs(getElementsByType("player")) do
            local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower()
            if name_:find(name, 1, true) then
                return player
            end
        end
    else
    return false
    end
end

function payScript(player,cmd,other,amount)     
   local name = getPlayerFromPartialName(other)
   local atg
   local rpm = getPlayerMoney(player)
   local err = {}
    
   if name == false then
   err[#err+1] = "Name not specified correctly."
   end
   
   if tonumber(amount) < 0 then
   err[#err+1] = "Amount can't be negative."
   end
 
   atg = tonumber(amount)
   
   if (rpm-atg) < 0 then
   err[#err+1] = "Given amount is more than yours."
   end
   
   if(player == name) then
   err[#err+1] = "You can't give it to yourself."
   end
   
   if #err == 0 then
   setPlayerMoney(name,atg)
   outputChatBox("Given:"..atg.." to:"..getPlayerName(name),player,0,255,0)
   setPlayerMoney(player,(rpm-atg))
   outputChatBox("Left:"..(rpm-atg).." to you.",player,0,255,0)
   else
   for i=1,#err do 
   outputChatBox(err[i],player,0,255,0)
   end
   end
   
end
addCommandHandler( "pay", payScript ) 

Try this

  • Like 1

Share this post


Link to post

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.