Jump to content

I need help to give money to players in race.


Mario222

Recommended Posts

 

I have a problem, this is a part of my resource that gives money to players based on their position, but instead of giving them money, it subtracts that amount, I don't know how to solve it.

function giveMoneyWinDie ( ) 
    local account = getPlayerAccount ( source ); 
    local playersAlive = getAliveGuys ( 1 ); 
    local playersDead = getDeadGuys ( ); 
    local checkYourPos = playersAlive + 1 
    local pos = nil; 
    if not (checkYourPos <= 0) then 
        if tonumber ( checkYourPos ) == 1 then pos = 1 else pos = checkYourPos; end 
        if tonumber ( checkYourPos ) == 2 then pos = 2; end 
        if pos == 1 or pos == 21 or pos == 31 then 
            posName = "st"; 
        elseif pos == 2 or pos == 22 or pos == 32 then 
            posName = "nd"; 
        elseif pos == 3 or pos == 23 or pos == 33 then 
            posName = "rd"; 
        else 
            posName = "th"; 
        end 
        if not getElementData ( source, "gotMoney" ) then 
            local money = math.ceil ( getPlayerCount ( ) * 50 / pos ); 
            givePlayerMoney ( source, money ); 
            outputChatBox ( "* You were #abcdef[#ff0000" .. pos .. posName .. "#abcdef]#ffffff and #abcdefearned #ffffff" .. money .. "#00ff00$!", source, 255, 255, 255, true ); 
            setElementData ( source, "gotMoney", true ); 
            if account then 
                setAccountData ( account, "money", tostring ( getPlayerMoney ( source ) ) ); 
            end 
        end 
    end 
end 
addEventHandler ( "onPlayerWasted", root , giveMoneyWinDie ) 

 

Edited by Mario222
Error
Link to comment

Hello Mario222!

Nice to see you in our MTA forums scripting section. It looks like your script is connected to other major components of your "race panel" that we have no insight into, especially the variable "playersAlive". Without that insight we cannot give a definite answer about this problem.

Did you know that givePlayerMoney does work with non-positive amounts aswell? It looks like a bug in the documentation. I have added a Remarks section with a note about the misbehaviour.

Do you know about the useful outputDebugString function? You can put it into your script to see what internal value combinations are occurring inside of it. You can login as admin into your server and use the "/debugscript 3" command to show a debug console with the messages. I suggest you to put the following and see what happens:

        if not getElementData ( source, "gotMoney" ) then 
            outputDebugString( "pos: " .. tostring(pos) );
            local money = math.ceil ( getPlayerCount ( ) * 50 / pos ); 
            outputDebugString( "money: " .. tostring(money) );
            givePlayerMoney ( source, money ); 
            outputChatBox ( "* You were #abcdef[#ff0000" .. pos .. posName .. "#abcdef]#ffffff and #abcdefearned #ffffff" .. money .. "#00ff00$!", source, 255, 255, 255, true ); 
            setElementData ( source, "gotMoney", true ); 
            if account then 
                outputDebugString( "player money: " .. tostring(getPlayerMoney(source)) );
                setAccountData ( account, "money", tostring ( getPlayerMoney ( source ) ) ); 
            end 
        end 

If you see any negative values in the debug console then let us know with a screenshot! ?

Edited by The_GTA
  • Like 1
Link to comment

addEventHandler ( 'onGamemodeMapStart', root,
    function ( mapres )
        local txMapName = getResourceName ( mapres );
        pHasBought = false;
        allGotVehicle = false;
        getAliveGuys ( 3 );
        saveSqlPlayed ( txMapName );
    end
)

function rStart ( )
    pHasBought = false;
    allGotVehicle = false;
    executeSQLQuery ( "CREATE TABLE IF NOT EXISTS tx_MapShop ( mapName TEXT, played INTEGER )" );
    getAllMapsFromManager ( );
    txBMaps = { }
end
addEventHandler ( "onResourceStart", getResourceRootElement ( getThisResource ( ) ), rStart )

function buyNextMap ( player, mapName )
    if not player or not mapName then return end
    if ( isGuestAccount ( getPlayerAccount ( player ) ) == false ) then
        if pHasBought == false then
            local money = tonumber ( getPlayerMoney ( player ) );
            local theTime = getMsFromMin ( 1 );
            if money >= tonumber ( price ) then
                if ( not txBMaps [ mapName ] ) then
                    txBMaps [ mapName ] = true
                    executeCommandHandler ( "bm", player, mapName ); --This function need admin rights
                    pHasBought = player;
                    setTimer ( removeMapFromList, theTime.ms, 1, mapName );
                else
                    outputChatBox ( "* #ff0000'#ffffff" .. mapName .. "#ff0000' #ffffffwill be #abcdefavailable #ffffffin #abcdef10 #ffffffminutes", player, 255, 255, 255, true );
                end
            else
                outputChatBox ( "* You #abcdefdon't #ffffffhave enough #abcdefmoney #ffffffto buy the map!", player, 255, 255, 255, true );
            end
        else
            outputChatBox ( "* A #abcdefmap #ffffffis already bought at the moment! Please #abcdeftry #ffffffagain later", player, 255, 255, 255, true );
        end
    else
        outputChatBox ( "* You should be #abcdeflogged in to buy the map!", player, 255, 255, 255, true );
    end
end
addEvent ( "doBuyMap", true )
addEventHandler ( "doBuyMap", root, buyNextMap )

function removeMapFromList ( mapName )
    outputChatBox ( "* #abcdef" .. mapName .. " #ffffff - #ffffffis now #abcdefavailable #ffffffat the #abcdefMap Shop!", root, 255, 255, 255, true );
    txBMaps [ mapName ] = nil;
end

addEvent ( "onRaceStateChanging", true )
addEventHandler ( "onRaceStateChanging", root,
    function ( newState )
        if  ( newState == "Running" ) then
            for k,v in pairs ( getElementsByType ( "player" ) ) do
                local hisVehicle = getPedOccupiedVehicle ( v );
                if not hisVehicle then return end
                setElementData (v, "gotMoney", false );
                if isPedInVehicle ( v ) then
                    allGotVehicle = true;
                end
            end
            if allGotVehicle then
                getAliveGuys ( 1 );
            end
        end
    end
)

addEventHandler ( "onPlayerJoin", root,
    function ( )
        setElementData ( source, "gotMoney", false );
        --Atm only setElementData
    end
)

--Functions by Admin Panel (lil_Toady)
function getAllMaps ( loadList, s )
    local tableOut
    if loadList then
        tableOut = { };
        local gamemodes = { };
        gamemodes = call ( getResourceFromName ( "mapmanager" ), "getGamemodes" );
        for id, gamemode in ipairs ( gamemodes ) do
            tableOut [ id ] = { };
            tableOut [ id ].name = getResourceInfo(gamemode,"name") or getResourceName(gamemode);
            tableOut [ id ].resname = getResourceName(gamemode);
            tableOut [ id ].maps = {};
            local maps = call ( getResourceFromName ( "mapmanager" ), "getMapsCompatibleWithGamemode", gamemode );
            for _, map in ipairs ( maps ) do
                table.insert ( tableOut [ id ][ "maps" ],{ name = getResourceInfo ( map, "name" ) or getResourceName ( map ),resname = getResourceName ( map ) } );
            end
            table.sort ( tableOut [ id ][ "maps" ],sortCompareFunction );
        end
        table.sort ( ( tableOut ), sortCompareFunction );
        table.insert ( tableOut, { name = "no gamemode", resname = "no gamemode", maps = { } } );
        local countGmodes = #tableOut;
        local maps = call ( getResourceFromName ( "mapmanager" ), "getMapsCompatibleWithGamemode" );
        for id, map in ipairs ( maps ) do
            table.insert ( tableOut [ countGmodes ][ "maps" ],{ name = getResourceInfo ( map, "name" ) or getResourceName ( map ), resname = getResourceName (map ) } );
        end
        table.sort ( tableOut [ countGmodes ][ "maps" ], sortCompareFunction );
    end
    local map = call ( getResourceFromName ( "mapmanager" ), "getRunningGamemodeMap" );
    local gamemode = call ( getResourceFromName ( "mapmanager" ), "getRunningGamemode" );
    gamemode = gamemode and getResourceName ( gamemode ) or "N/A";
    map = map and getResourceName ( map ) or "N/A";
    triggerClientEvent ( "refreshCompleted", loadList, tableOut, gamemode, map, s );
end
addEvent ( "doRefreshMapList", true )
addEventHandler ( "doRefreshMapList", root, getAllMaps )

function sortCompareFunction ( s1, s2 )
    if type ( s1 ) == "table" and type ( s2 ) == "table" then
        s1, s2 = s1.name, s2.name;
    end
    s1, s2 = s1:lower ( ), s2:lower( );
    if s1 == s2 then
        return false
    end
    local byte1, byte2 = string.byte ( s1:sub ( 1, 1 ) ), string.byte ( s2:sub ( 1, 1 ) );
    if not byte1 then
        return true
    elseif not byte2 then
        return false
    elseif byte1 < byte2 then
        return true
    elseif byte1 == byte2 then
        return sortCompareFunction ( s1:sub ( 2 ), s2:sub ( 2 ) );
    else
        return false
    end
end

function getAliveGuys() 
    local alivePlayers = 0 
    for index,player in ipairs(getElementsByType("player")) do 
        if getElementData(player,"state") == "alive" then 
            alivePlayers = alivePlayers + 1 
        end 
    end 
    return alivePlayers 
end 
  
  
function getDeadGuys() 
    local deadPlayers = 0 
    for index,player in ipairs(getElementsByType("player")) do 
        if getElementData(player,"state") == "dead" then 
            deadPlayers = deadPlayers + 1 
        end 
    end 
    return deadPlayers 
end 
  
function giveMoneyWinDie ( ) 
    local account = getPlayerAccount ( source ); 
    local playersAlive = getAliveGuys ( 1 ); 
    local playersDead = getDeadGuys ( ); 
    local checkYourPos = playersAlive + 1 
    local pos = nil; 
    if not (checkYourPos <= 0) then 
        if tonumber ( checkYourPos ) == 1 then pos = 1 else pos = checkYourPos; end 
        if tonumber ( checkYourPos ) == 2 then pos = 2; end 
        if pos == 1 or pos == 21 or pos == 31 then 
            posName = "st"; 
        elseif pos == 2 or pos == 22 or pos == 32 then 
            posName = "nd"; 
        elseif pos == 3 or pos == 23 or pos == 33 then 
            posName = "rd"; 
        else 
            posName = "th"; 
        end 
 if not getElementData ( source, "gotMoney" ) then 
            outputDebugString( "pos: " .. tostring(pos) );
            local money = math.ceil ( getPlayerCount ( ) * 50 / pos ); 
            outputDebugString( "money: " .. tostring(money) );
            givePlayerMoney ( source, money ); 
            outputChatBox ( "* You were #abcdef[#ff0000" .. pos .. posName .. "#abcdef]#ffffff and #abcdefearned #ffffff" .. money .. "#00ff00$!", source, 255, 255, 255, true ); 
            setElementData ( source, "gotMoney", true ); 
            if account then 
                outputDebugString( "player money: " .. tostring(getPlayerMoney(source)) );
                setAccountData ( account, "money", tostring ( getPlayerMoney ( source ) ) ); 
            end 
        end 
    end 
end 
addEventHandler ( "onPlayerWasted", root , giveMoneyWinDie ) 
 

 

I'm sorry for not giving complete info, it's a shopmap panel, and I tried to modify it to avoid giving negative numbers, that is, when a player loses in the first place, he gives him $ 50, but instead of giving them the $ 50, he You remove them, and if you have 0, you end up with -50 $.

Link to comment

Thank you for sharing the script with us! From the script excerpt that you have given us there appears to be no problem. I have several questions to you:

  1. Is there another script that does use the givePlayerMoney or takePlayerMoney function running on your server?
  2. Why do you pass the parameter "1" to getAliveGuys? Is there a second function definition somewhere that is different?
  3. Did you collect debug logs?
  • Like 1
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...