Turbo777

How to fix this in the pay script?

9 posts in this topic

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?

Share this post


Link to post

Posted (edited)

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

Share this post


Link to post
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 

 

Share this post


Link to post

Posted (edited)

Alright, try changing

if (string.count(amount,"-")) then

to this:

if (string.count(tostring(amount),"-")) then

 

Edited by knightscript

Share this post


Link to post
9 minutes ago, knightscript said:

Alright, try changing


if (string.count(amount,"-")) then

to this:


if (string.count(tostring(amount),"-")) then

 

Ok, now i got this error: attempt to index global 'str' (a nil value) O.o

Share this post


Link to post

Alright, one last thing, this should do the trick,

replace this:

amount = str:gsub('%A',amount)

with this:

amount = string.gsub('%A',amount)

 

:)

Share this post


Link to post

Posted (edited)

you should test all the bugs with this script. Some of the players like using bugs.

Try to pay /pay NAME 4664.4442 if is this work, you should fix it

Edited by TheMOG

Share this post


Link to post

You don't have to make things harder

simple you can do that

if amount < 0 then outputChatBox("You can't use minus") return end

 

Share this post


Link to post

Posted (edited)

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

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.