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.
答案 0 :(得分:1)
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).."');" )
问题出在表达式mapfinishes+1
中,似乎在没有mapfinishes
设置的情况下执行了。这意味着上面的if
循环没有执行,因为row
是nil
或false
。请记住,在Lua中,零和空字符串是真值。
另一种可能性是row["Mapfinishes"]
本身为nil
,因此mapfinishes
仍为nil
。
通常,最小/无全局变量会更好。如果您仅在此功能中使用mapfinishes
,则应将其声明为local
。