Jump to content

Givemoney


Recommended Posts

Hi, When i try to send someone money it doesnt output in chat box the text for the local player, but the target player can see the output, It suppose to write in chatbox ("You gave "..money.." to "..getPlayerName(targetPlayer)", source, r, g, b)

The problem is that the target player can recieve the text of the money but who send money it doesnt output nothing, here is the code

function giveSomeoneMoney(player, cmd, target, amount) 
    if target then 
        if amount then 
            local money = getPlayerMoney(player) 
            local targetplayer = getPlayerFromParticalName(target) 
            local r,g,b = getPlayerNametagColor(source) 
            amount = tonumber(amount) 
            if targetplayer then 
                if money >= amount then 
                    givePlayerMoney(targetplayer, amount) 
                    takePlayerMoney(player, amount) 
                    outputChatBox("You have been given #00FF00$"..amount.." #0096FF from #FFFFFF"..getPlayerName(player) , targetplayer, 0, 81, 255, true) 
                    outputChatBox("You gave #00FF00$"..amount.."#0096FF to #FFFFFF"..getPlayerName(target), player, 0, 81, 255, true) 
                else 
                    outputChatBox("You don't have enought money!", player, 255, 0, 0) 
                end 
            else 
                outputChatBox("Error: Player not found", player, 255, 0, 0) 
            end 
        else 
            outputChatBox("Error: /givemoney [player name] [amount]", player, 255, 0, 0) 
        end 
    else  
        outputChatBox("Error: /givemoney [player name] [amount]", player, 255, 0, 0) 
    end 
end 
addCommandHandler("givemoney", giveSomeoneMoney) 
  
function getPlayerFromParticalName(thePlayerName) 
    local thePlayer = getPlayerFromName(thePlayerName) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs(getElementsByType("player")) do 
        if string.find(string.gsub(getPlayerName(thePlayer):lower(),"#%x%x%x%x%x%x", ""), thePlayerName:lower(), 1, true) then 
            return thePlayer 
        end 
    end 
return false 
end 
  
  

Link to comment
  • MTA Team
outputChatBox("You gave #00FF00$"..amount.."#0096FF to #FFFFFF"..getPlayerName(target) 

getPlayerName(target) 

Use targetplayer instead of target, because you pass the command parameter name to the function:

Example usage:

/givemoney Franklin234 500 

That line produces the following code:

outputChatBox("You gave #00FF00$".."500".."#0096FF to #FFFFFF"..getPlayerName("Franklin234") 

Link to comment
  • MTA Team
outputChatBox("You gave #00FF00$"..tostring(amount).."#0096FF to #FFFFFF"..getPlayerName(target)) 

Again the wrong variable:

outputChatBox("You gave #00FF00$"..tostring(amount).."#0096FF to #FFFFFF"..getPlayerName(targetplayer)) 

Link to comment
But you cannot set a string with a number in Lua, you must convert the number to string:

outputChatBox("You gave #00FF00$"..tostring(amount).."#0096FF to #FFFFFF"..getPlayerName(target));

Do not argue just test it before being naive.

Line 12 is working fine for target player Without converting it, The problem is that it doesnt show for the player who sent the money....

Link to comment
  • 2 weeks later...
function giveSomeoneMoney(player, cmd, target, amount) 
    if target then 
        if amount then 
            local money = getPlayerMoney(player) 
            local targetplayer = getPlayerFromParticalName(target) 
            local r,g,b = getPlayerNametagColor(player) 
            amount = tonumber(amount) 
            if targetplayer then 
                if money >= amount then 
                    givePlayerMoney(targetplayer, amount) 
                    takePlayerMoney(player, amount) 
                    outputChatBox("You have been given #00FF00$"..tostring(amount).." #0096FF from #FFFFFF"..getPlayerName(player) , targetplayer, 0, 81, 255, true) 
                    outputChatBox("You gave #00FF00$"..tostring(amount).."#0096FF to #FFFFFF"..getPlayerName(targetplayer), player, 0, 81, 255, true) 
                else 
                    outputChatBox("You don't have enought money!", player, 255, 0, 0) 
                end 
            else 
                outputChatBox("Error: Player not found", player, 255, 0, 0) 
            end 
        else 
            outputChatBox("Error: /givemoney [player name] [amount]", player, 255, 0, 0) 
        end 
    else 
        outputChatBox("Error: /givemoney [player name] [amount]", player, 255, 0, 0) 
    end 
end 
addCommandHandler("givemoney", giveSomeoneMoney) 
  
function getPlayerFromParticalName(thePlayerName) 
    local thePlayer = getPlayerFromName(thePlayerName) 
    if thePlayer then 
        return thePlayer 
    end 
    for _,thePlayer in ipairs(getElementsByType("player")) do 
        if string.find(string.gsub(getPlayerName(thePlayer):lower(),"#%x%x%x%x%x%x", ""), thePlayerName:lower(), 1, true) then 
            return thePlayer 
        end 
    end 
return false 
end 

Did everything what they said with your script and it works.

EDIT: Edited it after forgetting to change a line back to normal.

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