Jump to content

How to fix this in the pay script?


Turbe$Z

Recommended Posts

function findPlayer(name)
	local matches = {}
	for i,v in ipairs(getElementsByType("player")) do
		if getPlayerName(v) == name then
			return v
		end
		local playerName = getPlayerName(v):gsub("#%x%x%x%x%x%x", "")
		playerName = playerName:lower()
		if playerName:find(name:lower(), 0) then
			table.insert(matches, v)
		end
	end
	if #matches == 1 then
		return matches[1]
	end
	return false
end

addCommandHandler("pay",
	function(player, cmd, name, amount)
		local amount = tonumber(amount)
		if name and amount then
			local target = findPlayer(name)
			local money = getPlayerMoney(target)
			if money >= amount then
				takePlayerMoney(player, amount)
				givePlayerMoney(target, amount)
				outputChatBox("#FFffFF Átutaltál neki: #c8c8c8" .. getPlayerName(target) .. " #0088ff" .. amount .. " Forintot.", player, 0, 255, 0, true)
				outputChatBox("#c8c8c8 " .. getPlayerName(player) .. " #FFffFFutalt neked #0088ff" .. amount .. " Forintot.", target, 0, 255, 0, true)
			else
				outputChatBox("#FFffFF Nincs elég pénzed.", target, 255, 0, 0, true)
			end
		end
	end
)

why can i 'pay' minus amount? how to fix this?

Link to comment

You could use string.count to check if "amount" contains a negative "-" symbol, and use Split to remove it from the string, 

EDIT:

function string.count (text, search)
	if ( not text or not search ) then return false end
	
	return select ( 2, text:gsub ( search, "" ) );
end

addCommandHandler("pay",
	function(player, cmd, name, amount)
		local amount = tonumber(amount)
		if name and amount then
			local target = findPlayer(name)
			local money = getPlayerMoney(target)
      		if (string.count(amount,"-")) then
        		amount = str:gsub('%A',amount)
        	end
			if money >= amount then
				takePlayerMoney(player, amount)
				givePlayerMoney(target, amount)
				outputChatBox("#FFffFF Átutaltál neki: #c8c8c8" .. getPlayerName(target) .. " #0088ff" .. amount .. " Forintot.", player, 0, 255, 0, true)
				outputChatBox("#c8c8c8 " .. getPlayerName(player) .. " #FFffFFutalt neked #0088ff" .. amount .. " Forintot.", target, 0, 255, 0, true)
			else
				outputChatBox("#FFffFF Nincs elég pénzed.", target, 255, 0, 0, true)
			end
		end
	end
)

Try this

 

Edited by knightscript
Added code
Link to comment
27 minutes ago, knightscript said:

You could use string.count to check if "amount" contains a negative "-" symbol, and use Split to remove it from the string, 

EDIT:


function string.count (text, search)
	if ( not text or not search ) then return false end
	
	return select ( 2, text:gsub ( search, "" ) );
end

addCommandHandler("pay",
	function(player, cmd, name, amount)
		local amount = tonumber(amount)
		if name and amount then
			local target = findPlayer(name)
			local money = getPlayerMoney(target)
      		if (string.count(amount,"-")) then
        		amount = str:gsub('%A',amount)
        	end
			if money >= amount then
				takePlayerMoney(player, amount)
				givePlayerMoney(target, amount)
				outputChatBox("#FFffFF Átutaltál neki: #c8c8c8" .. getPlayerName(target) .. " #0088ff" .. amount .. " Forintot.", player, 0, 255, 0, true)
				outputChatBox("#c8c8c8 " .. getPlayerName(player) .. " #FFffFFutalt neked #0088ff" .. amount .. " Forintot.", target, 0, 255, 0, true)
			else
				outputChatBox("#FFffFF Nincs elég pénzed.", target, 255, 0, 0, true)
			end
		end
	end
)

Try this

 

function findPlayer(name)
	local matches = {}
	for i,v in ipairs(getElementsByType("player")) do
		if getPlayerName(v) == name then
			return v
		end
		local playerName = getPlayerName(v):gsub("#%x%x%x%x%x%x", "")
		playerName = playerName:lower()
		if playerName:find(name:lower(), 0) then
			table.insert(matches, v)
		end
	end
	if #matches == 1 then
		return matches[1]
	end
	return false
end

function string.count (text, search)
	if ( not text or not search ) then return false end
	return select ( 2, text:gsub ( search, "" ) );
end

addCommandHandler("pay",
	function(player, cmd, name, amount)
		local amount = tonumber(amount)
		if name and amount then
			local target = findPlayer(name)
			local money = getPlayerMoney(target)
      		if (string.count(amount,"-")) then
        		amount = str:gsub('%A',amount)
        	end
			if money >= amount then
				takePlayerMoney(player, amount)
				givePlayerMoney(target, amount)
				outputChatBox("#FFffFF Átutaltál neki: #c8c8c8" .. getPlayerName(target) .. " #0088ff" .. amount .. " Forintot.", player, 0, 255, 0, true)
				outputChatBox("#c8c8c8 " .. getPlayerName(player) .. " #FFffFFutalt neked #0088ff" .. amount .. " Forintot.", target, 0, 255, 0, true)
			else
				outputChatBox("#FFffFF Nincs elég pénzed.", target, 255, 0, 0, true)
			end
		end
	end
)

doesn't working :/ 

error: attempt to index local 'text' (a number value) :S 

 

Link to comment

why don't you just verify if the amount is positive and bigger than 0

if ( amount > 0 and math.ceil( amount ) == amount --[[ verify if the amount is bigger than 0 and is an integer( doesn't contain decimals )]] ) then 
  --code
else 
  outputChatBox( "The amount must be an integer bigger than 0" )
end

 

Edited by *BeaT*
added else
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...