Reputation: 17
I'm trying to set up something in a game that runs off of Lua and this specific local function is triggered when the player finishes the map. Here is the code:
local function setDrRanks( ply )
local name = SQLStr( ply:Nick() )
local sid = ply:SteamID()
drsql:query( "SELECT MapFinishes from dr_exp WHERE SteamID = '"..sid.."'", function( q, data )
local row = data[1]
if ( row ) then
mapfinishes = row["Mapfinishes"]
end
drsql:query( "REPLACE into dr_exp (`SteamID`, `PlayerName`, `MapFinishes`) VALUES('"..sid.."', "..name..", '"..(mapfinishes+1).."');" );
end )
end
The function is to insert into SQL via a lua function, which it did successfully when ran the first time, as the player was at 0 finishes. Once they hit 1, it refused to do a simple +1 on the mapfinishes value. Which strange is that this seems to work 100% when the player is at 0 finishes, and it will put them at 1, but once they are at 1, it will not add to it any longer. The error received is:
attempt to perform arithmetic on global 'mapfinishes' (a nil value)
Anyone have any ideas? Thanks in advance.
Upvotes: 0
Views: 16489
Reputation: 32954
local row = data[1]
if ( row ) then
mapfinishes = row["Mapfinishes"]
end
drsql:query( "REPLACE into dr_exp (`SteamID`, `PlayerName`, `MapFinishes`) VALUES('"..sid.."', "..name..", '"..(mapfinishes+1).."');" )
The issue is in the expression mapfinishes+1
which seems to have gotten executed without mapfinishes
getting set. This implies that the if
loop above didn't execute because row
was nil
or false
. Remember, in Lua, zero and the empty string are truth values.
Another possibility is that the row["Mapfinishes"]
itself was nil
so that mapfinishes
remains nil
.
Usually it's better to have minimal/no global variables. If you're going to use mapfinishes
only within this function, it'd be appropriate to declare it local
.
Upvotes: 1