Jump to content

Solved


Newbie

Recommended Posts

  
function buyMap(player, command,  ...) 
    local account = ( ( getPlayerAccount( player ) and not isGuestAccount( getPlayerAccount( player ) ) ) and getPlayerAccount( player ) or false ) 
    if ( account ) then 
    local cashHave = tonumber( getAccountData( account, "cash" ) ) 
    if g_ForcedNextMap then 
        outputChatBox( 'Next map is ' .. getMapName( g_ForcedNextMap ), player ) 
        return 
       end 
    end 
    local query = #{...}>0 and table.concat({...},' ') or nil 
    if not query then 
        if g_ForcedNextMap then 
            outputChatBox( 'Next map is ' .. getMapName( g_ForcedNextMap ), player ) 
        else 
            outputChatBox( 'Next map is not set', player, 255, 0, 0 ) 
        end 
        return 
    end 
        local map = findMap(query) 
        if not map then 
            outputChatBox(errormsg, player) 
            return 
        end 
        if(cashHave >= 10) then 
            if lastmap_B == map then 
            outputChatBox( 'That map has been played too much recently.', player, 255, 0, 0 ) 
        else 
            g_ForcedNextMap = map 
            outputChatBox(getPlayerName(player).. " bought map '" ..getMapName(g_ForcedNextMap).. "' for $10. (/bm)", g_Root, 0, 240, 0) 
            setAccountData( account, "cash", cashHave - 10 ) 
            lastmap_B = g_ForcedNextMap 
        end 
    else 
        outputChatBox("You don't have enough money. ($10)", player, 255, 0, 0) 
    end 
end 
addCommandHandler('bm', buyMap) 
addCommandHandler('buymap', buyMap) 
  

Link to comment
Which line number in this script is it?

Fixed that.

-- Buy next map 
  
function buyMap(player, command,  ...) 
    local account = ( ( getPlayerAccount( player ) and not isGuestAccount( getPlayerAccount( player ) ) ) and getPlayerAccount( player ) or false ) 
    if ( account ) then 
    local cashHave = tonumber( getAccountData( account, "cash" ) ) 
    if g_ForcedNextMap then 
        outputChatBox( 'Next map is ' .. getMapName( g_ForcedNextMap ), player ) 
        return 
       end 
    end 
    local query = #{...}>0 and table.concat({...},' ') or nil 
    if not query then 
        if g_ForcedNextMap then 
            outputChatBox( 'Next map is ' .. getMapName( g_ForcedNextMap ), player ) 
        else 
            outputChatBox( 'Next map is not set', player, 255, 0, 0 ) 
        end 
        return 
    end 
        local map = findMap(query) 
        if not map then 
            outputChatBox(errormsg, player) 
            return 
        end 
        if ( cashHave ) and ( cashHave >= 1 ) then 
            if lastmap_B == map then 
            outputChatBox( 'That map has been played too much recently.', player, 255, 0, 0 ) 
        else 
            g_ForcedNextMap = map 
            outputChatBox(getPlayerName(player).. " bought map '" ..getMapName(g_ForcedNextMap).. "' for $1. (/bm)", g_Root, 0, 240, 0) 
            setAccountData( account, "cash", cashHave - 1 ) 
            lastmap_B = g_ForcedNextMap 
        end 
    else 
        outputChatBox("You don't have enough money. ($1)", player, 255, 0, 0) 
    end 
end 
addCommandHandler('bm', buyMap) 
addCommandHandler('buymap', buyMap) 
  

I have cash. But the script outputs that i dont. "You don't have enough money. ($1)"

Link to comment
  • Moderators

Hi,

The "cashHave" variable is defined inside the "if ( account ) then" scope (line 5) and it doesn't exist anymore out of this scope (when the code reaches the "end" keyword of that scope)

So here is a better implementation:

-- Buy next map 
  
function buyMap(player, command,  ...) 
    local account = ( ( getPlayerAccount( player ) and not isGuestAccount( getPlayerAccount( player ) ) ) and getPlayerAccount( player ) or false ) 
    if ( account ) then 
        local cashHave = tonumber( getAccountData( account, "cash" ) ) 
        if g_ForcedNextMap then 
            outputChatBox( 'Next map is ' .. getMapName( g_ForcedNextMap ), player ) 
            return 
        end 
        local query = #{...}>0 and table.concat({...},' ') or nil 
        if not query then 
            if g_ForcedNextMap then 
                outputChatBox( 'Next map is ' .. getMapName( g_ForcedNextMap ), player ) 
            else 
                outputChatBox( 'Next map is not set', player, 255, 0, 0 ) 
            end 
            return 
        end 
        local map = findMap(query) 
        if not map then 
            outputChatBox(errormsg, player) 
            return 
        end 
        if ( cashHave ) and ( cashHave >= 1 ) then 
            if lastmap_B == map then 
                outputChatBox( 'That map has been played too much recently.', player, 255, 0, 0 ) 
            else 
                g_ForcedNextMap = map 
                outputChatBox(getPlayerName(player).. " bought map '" ..getMapName(g_ForcedNextMap).. "' for $1. (/bm)", g_Root, 0, 240, 0) 
                setAccountData( account, "cash", cashHave - 1 ) 
                lastmap_B = g_ForcedNextMap 
            end 
        else 
            outputChatBox("You don't have enough money. ($1)", player, 255, 0, 0) 
        end 
    end 
end 
addCommandHandler('bm', buyMap) 
addCommandHandler('buymap', buyMap) 

So now the condition is in the same scope as the variable.

Tips:

- Take care with indentation

- Parenthesis are optional (they just are used for priorities)

if ( cashHave ) and ( cashHave >= 1 ) then 

can be written like this:

if cashHave and cashHave >= 1 then 

Best regards,

Citizen

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