Jump to content

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


DreaM40BG

Recommended Posts

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

Link to comment

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 
) 

 

Link to comment
[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 
) 

 

Link to comment
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 
) 

 

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

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

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 

 

Link to comment
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 ;/ 

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