Newbie Posted February 13, 2014 Share Posted February 13, 2014 (edited) SOLVED AGES AGO Edited March 17, 2014 by Guest Link to comment
Anubhav Posted February 13, 2014 Share Posted February 13, 2014 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
Newbie Posted February 13, 2014 Author Share Posted February 13, 2014 Its fine now. When i try tu buy map i get this: [2014-02-12 12:51:18] ERROR: race\racevoting_server.lua:782: attempt to compare number with nil Link to comment
Anubhav Posted February 13, 2014 Share Posted February 13, 2014 Which line number in this script is it? Link to comment
Newbie Posted February 13, 2014 Author Share Posted February 13, 2014 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
Anubhav Posted February 13, 2014 Share Posted February 13, 2014 Are you sure? Try using /buymap MapName If it does not work. add me on skype anubhav.agarwal87 Link to comment
Newbie Posted February 13, 2014 Author Share Posted February 13, 2014 Are you sure? Try using /buymap MapNameIf it does not work. add me on skype anubhav.agarwal87 I tryed that. Added. Link to comment
Newbie Posted February 13, 2014 Author Share Posted February 13, 2014 Any more suggestions ? Link to comment
Newbie Posted February 13, 2014 Author Share Posted February 13, 2014 Please help Guys Link to comment
Moderators Citizen Posted February 13, 2014 Moderators Share Posted February 13, 2014 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
Newbie Posted February 13, 2014 Author Share Posted February 13, 2014 I feel so dumb now Thanks! Link to comment
Moderators Citizen Posted February 13, 2014 Moderators Share Posted February 13, 2014 No problem, don't forget to remove the "Not" from "Not solved :/" Regards, Citizen Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now