Turbe$Z Posted April 21, 2017 Posted April 21, 2017 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? My Fun server: My DD server:
knightscript Posted April 21, 2017 Posted April 21, 2017 (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 April 21, 2017 by knightscript Added code My Scripts: Custom vehicle by ACL Custom vehicle by Account
Turbe$Z Posted April 21, 2017 Author Posted April 21, 2017 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) My Fun server: My DD server:
knightscript Posted April 21, 2017 Posted April 21, 2017 (edited) Alright, try changing if (string.count(amount,"-")) then to this: if (string.count(tostring(amount),"-")) then Edited April 21, 2017 by knightscript My Scripts: Custom vehicle by ACL Custom vehicle by Account
Turbe$Z Posted April 21, 2017 Author Posted April 21, 2017 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) My Fun server: My DD server:
knightscript Posted April 21, 2017 Posted April 21, 2017 Alright, one last thing, this should do the trick, replace this: amount = str:gsub('%A',amount) with this: amount = string.gsub('%A',amount) My Scripts: Custom vehicle by ACL Custom vehicle by Account
itHyperoX Posted April 22, 2017 Posted April 22, 2017 (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 April 22, 2017 by TheMOG
#BrosS Posted April 22, 2017 Posted April 22, 2017 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 “من أراد الفشل عليه بالنجاح”
SheriFF Posted April 22, 2017 Posted April 22, 2017 (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 April 22, 2017 by *BeaT* added else
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now