Jump to content

[HELP] Bug with /give <nick> <money> [IMPORTANT]


DreaM40BG

Recommended Posts

Posted

Hello, I was trying for 30 minutes to fix that problem and I couldn't. I tried to see the MTA wiki, still I didn't get it.

The problem is: For example ,there is no player "test" online in the server and I write /give test 2 and it takes 2$ from me and gives to nobody. How to fix that?

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("give", 
    function(player, cmd, name, amount) 
        local amount = tonumber(amount) 
        if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix
            local target = findPlayer(name) 
            local money = getPlayerMoney(player) 
			local targetid = getPlayerFromName(target) or outputChatBox("Player not found", player) 
            if money >= amount then 
                takePlayerMoney(player, amount) 
                givePlayerMoney(target, amount) 
                outputChatBox("You gave " .. getPlayerName(target) .. " $" .. amount .. ".", player, 0, 255, 0, false) 
                outputChatBox(getPlayerName(player) .. " gave you $" .. amount .. ".", target, 0, 255, 0, false) 
            else 
                outputChatBox("You do not have enough money.", player, 255, 0, 0, false) 
            end 
        end 
    end 
) 

I tried with "playerid" and others and still not working. :/

Posted
14 minutes ago, DreaM40BG said:

 

Don't work.

Any error Shown in debug?

2 hours ago, DreaM40BG said:
  • end
  • end
  • end
  • )

I think you forgot to add one more end ? 

You should add 4 ends not 3 

Posted

Yep, I did. Still not working, it doesn't send to anybody.

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("give", 
    function(player, cmd, name, amount) 
        local amount = tonumber(amount) 
        if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix
            local target = findPlayer(name) 
            local money = getPlayerMoney(player) 
			local targetid = getPlayerFromName(target)
            if targetid then 
            if money >= amount then 
                takePlayerMoney(player, amount) 
                givePlayerMoney(target, amount) 
                outputChatBox("You gave " .. getPlayerName(target) .. " $" .. amount .. ".", player, 0, 255, 0, false) 
                outputChatBox(getPlayerName(player) .. " gave you $" .. amount .. ".", target, 0, 255, 0, false) 
            else 
                outputChatBox("You do not have enough money.", player, 255, 0, 0, false) 
		    end
        end 
    end 
 end 
) 

 

Posted
[20-04-18 17:10:26] WARNING: givemoney/server.Lua:25: Bad argument @ 'getPlayerFromName' [Expected string at argument 1, got player]

Not working. :<

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("give", 
    function(player, cmd, name, amount) 
        local amount = tonumber(amount) 
        if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix
            local target = findPlayer(name) 
            local money = getPlayerMoney(player) 
			local targetid = getPlayerFromName(target)
            if targetid ~= false then 
            if money >= amount then 
                takePlayerMoney(player, amount) 
                givePlayerMoney(target, amount) 
                outputChatBox("You gave " .. getPlayerName(target) .. " $" .. amount .. ".", player, 0, 255, 0, false) 
                outputChatBox(getPlayerName(player) .. " gave you $" .. amount .. ".", target, 0, 255, 0, false) 
            else 
                outputChatBox("You do not have enough money.", player, 255, 0, 0, false) 
		    end
        end 
    end 
 end 
) 

 

Posted
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("give", 
    function(player, cmd, name, amount) 
        local amount = tonumber(amount) 
        if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix
            local targetid = findPlayer(name) 
            local money = getPlayerMoney(player) 
            if targetid ~= false then 
            if money >= amount then 
                takePlayerMoney(player, amount) 
                givePlayerMoney(target, amount) 
                outputChatBox("You gave " .. getPlayerName(target) .. " $" .. amount .. ".", player, 0, 255, 0, false) 
                outputChatBox(getPlayerName(player) .. " gave you $" .. amount .. ".", target, 0, 255, 0, false) 
            else 
                outputChatBox("You do not have enough money.", player, 255, 0, 0, false) 
		    end
        end 
    end 
 end 
) 

 

Posted (edited)
17 minutes ago, DreaM40BG said:

[20-04-18 17:10:26] WARNING: givemoney/server.Lua:25: Bad argument @ 'getPlayerFromName' [Expected string at argument 1, got player]

Not working. :<


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("give", 
    function(player, cmd, name, amount) 
        local amount = tonumber(amount) 
        if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix
            local target = findPlayer(name) 
            local money = getPlayerMoney(player) 
			local targetid = getPlayerFromName(target)
            if targetid ~= false then 
            if money >= amount then 
                takePlayerMoney(player, amount) 
                givePlayerMoney(target, amount) 
                outputChatBox("You gave " .. getPlayerName(target) .. " $" .. amount .. ".", player, 0, 255, 0, false) 
                outputChatBox(getPlayerName(player) .. " gave you $" .. amount .. ".", target, 0, 255, 0, false) 
            else 
                outputChatBox("You do not have enough money.", player, 255, 0, 0, false) 
		    end
        end 
    end 
 end 
) 

 

target is the player you're looking for, no need to get him from name

			local targetid = target

 

 

Edited by Prioq
Posted (edited)

Okay it takes money but don't give to the 2nd player. Not working, also no messages.

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("give", 
    function(player, cmd, name, amount) 
        local amount = tonumber(amount) 
        if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix
            local targetid = findPlayer(name) 
            local money = getPlayerMoney(player) 
            if targetid ~= false then 
            if money >= amount then 
                takePlayerMoney(player, amount) 
                givePlayerMoney(target, amount) 
                outputChatBox("You gave " .. getPlayerName(target) .. " $" .. amount .. ".", player, 0, 255, 0, false) 
                outputChatBox(getPlayerName(player) .. " gave you $" .. amount .. ".", target, 0, 255, 0, false) 
            else 
                outputChatBox("You do not have enough money.", player, 255, 0, 0, false) 
		    end
        end 
    end 
 end 
) 

 

Edited by DreaM40BG
Posted

i dont understand why you need the findPlayer function you could do like that

  
addCommandHandler("give", 
    function(player, cmd, name, amount) 
        local amount = tonumber(amount) 
        if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix
            local target = getPlayerFromName (name) 
            local money = getPlayerMoney(player) 
            if target ~= false then 
            if money >= amount then 
                takePlayerMoney(player, amount) 
                givePlayerMoney(target, amount) 
                outputChatBox("You gave " .. getPlayerName(target) .. " $" .. amount .. ".", player, 0, 255, 0, false) 
                outputChatBox(getPlayerName(player) .. " gave you $" .. amount .. ".", target, 0, 255, 0, false) 
            else 
                outputChatBox("You do not have enough money.", player, 255, 0, 0, false) 
		    end
        end 
    end 

 

Posted
13 minutes ago, Spakye said:

i dont understand why you need the findPlayer function you could do like that


  
addCommandHandler("give", 
    function(player, cmd, name, amount) 
        local amount = tonumber(amount) 
        if name and amount and amount > 0 and math.floor(amount) == amount then -- bugfix
            local target = getPlayerFromName (name) 
            local money = getPlayerMoney(player) 
            if target ~= false then 
            if money >= amount then 
                takePlayerMoney(player, amount) 
                givePlayerMoney(target, amount) 
                outputChatBox("You gave " .. getPlayerName(target) .. " $" .. amount .. ".", player, 0, 255, 0, false) 
                outputChatBox(getPlayerName(player) .. " gave you $" .. amount .. ".", target, 0, 255, 0, false) 
            else 
                outputChatBox("You do not have enough money.", player, 255, 0, 0, false) 
		    end
        end 
    end 

 

It is not working again ;/ 

Posted (edited)

what error does it give ?

edit : i notice i forgot  ) at then end of the code, if you copy pasted my code then add ) at the end

Edited by Spakye
  • Moderators
Posted

I told you before, learn Lua basics please before you ask in this section!

Some mistake what I suddenly found
DCeomq6.png

  • Like 1

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