Loading data from MYSQL sometimes doesn't work, resetting all the player's data
2 replies, posted
I've been having this issue for a few weeks and no matter what I do I cannot fix it. This is a really bad problem because if players keep having their stuff reset, no one is going to want to play. I'm also having to close the server everytime until the next "supposed" fix. I don't know what else I can do so now im at the mercy of facepunch or if need be, Gmodstore.
Here's how it should be going. It checks the database for a player's id, and then the stuff tied to that. Once it does, it should then set a player's NWVars to those found in the database. This works almost all the time. However, eventually and rarely, it will fail to get the correct data and end up loading 0s and 1s instead, effectively resetting their data.
I dont have any console logs for this because everytime this has happened ive been sleeping, and even so its hard to find out when it happens. This is assuming there even is an error when this occurs.
So, here is the basic code for the loading and saving. It's a mess but I need to know what exactly I am doing wrong. I've tried to look at pointshop as a reference but I dont think that has worked out for me.
function Checklevel(ply,callback) -- Set player stats
GetDataStuff(ply, function(exp, level, slots, metal)
ply:SetNWInt("xp",exp)
ply:SetNWInt("level",level)
ply:SetNWInt("slots",slots)
ply:SetNWInt("metal",metal)
ply:ChatPrint("[Racing Attack] Data loaded!")
end)
end
function GetDataStuff(ply, callback) -- Get player stats
local query = radb:query("SELECT * FROM `raceattack` WHERE `id` = "..ply:SteamID64())
function query:onSuccess(sqldata)
if #sqldata > 0 then -- Found data in database
local data = sqldata[1]
local xp = data.experience
local level = data.level
local slots = data.slots
local metal = data.metal
callback(data.experience, data.level, data.slots, data.metal)
else -- No data has been found
print(ply:Nick().."'s level start.")
ply:SetNWInt("xp",0)
ply:SetNWInt("level",0)
ply:SetNWInt("slots",4)
ply:SetNWInt("metal",0)
local query = radb:query("INSERT INTO raceattack VALUES ('"..ply:SteamID64().."', '"..ply:GetNWInt("level").."', '"..ply:GetNWInt("xp").."', '"..ply:GetNWInt("slots").."', '"..ply:GetNWInt("metal").."')")
query:start()
end
end
function query:onError(err, sql)
print("An error occured while executing the query: " .. err .. " (" .. sql .. ")")
query:start()
end
query:start()
end
function Savelevel(ply) -- Save player stats
local query = radb:query("SELECT * FROM `raceattack` WHERE `id` = "..ply:SteamID64())
function query:onSuccess(sqldata)
local query = radb:query("UPDATE `raceattack` SET `level` = '"..ply:GetNWInt("level").."' WHERE `id` = "..ply:SteamID64())
function query:onSuccess(sqldata)
end
function query:onError(err)
print("An error occured while executing the query: " .. err)
end
query:start()
local query = radb:query("UPDATE `raceattack` SET `experience` = '"..ply:GetNWInt("xp").."' WHERE `id` = "..ply:SteamID64())
function query:onSuccess(sqldata)
end
function query:onError(err)
print("An error occured while executing the query: " .. err)
end
query:start()
local query = radb:query("UPDATE `raceattack` SET `slots` = '"..ply:GetNWInt("slots").."' WHERE `id` = "..ply:SteamID64())
function query:onSuccess(sqldata)
end
function query:onError(err)
print("An error occured while executing the query: " .. err)
end
query:start()
local query = radb:query("UPDATE `raceattack` SET `metal` = '"..ply:GetNWInt("metal").."' WHERE `id` = "..ply:SteamID64())
function query:onSuccess(sqldata)
end
function query:onError(err)
print("An error occured while executing the query: " .. err)
end
query:start()
end
query:start()
end
I'm not here for responses on how to make the code prettier and faster, I'm here for things that will point me in the right direction. (If it both optimizes and points into the fix, thats fine)
If there is nothing apparently wrong here, then I'm going to have to dig much deeper into how these functions are used then.
If you are doing the data check on playerinitialspawn that may be your issue. Sometimes that hook will run a second or two prior to the players steamid becoming valid. I would do validity checks for steamid and all networked variables before executing anything else.
The check is done after a player sends a net message to the server, which starts from GM/InitPostEntity.
There is definitely plenty of time for the player to be valid.
Sorry, you need to Log In to post a reply to this thread.