• Lua File not Working Unless Auto Refreshed
    4 replies, posted
I have a Lua file I put together that tracks user's kills in TTT. It saves this data to a SQL database and the code itself is based off the TTT Stats addon on the workshop. The problem is that kills are not tracked unless I open and save the file, triggering the auto-refresh. I cannot for the life of me figure out why this is happening. I am a bit new to Lua but experienced in other languages. The code is just used on a small server to play with friends and it the file itself is in autorun/server.
Post the code?
that could happen when youre trying to use an entity or variable too early, when it does not exists yet. i guess youre trying to get ttt kills before shit is loaded
include("database.lua") print("KILL STATS: STARTED") if(db:ping())then print("KILL STATS: PING SUCCESS") end if SERVER then function new_player1( SteamID, ply )   steamID = SteamID name = ply:Name() local query = db:query( "INSERT INTO ttt_kills (`unique_id`, `name`, `Kills`, `TerKills`, `InnoKills`, `InnoInno`, `KarmaHigh`, `KarmaLow`, `Deaths`)VALUES ('"..steamID.."', '"..name.."', '0', '0', '0', '0', '1000', '1000', '0')" ) local result = db:query( "SELECT unique_id FROM ttt_kills  WHERE unique_id = '"..steamID.."'" ) query:start() result:start() function result:onSuccess(data) local row = data[1] for k,v in pairs(row) do print(v) end end function query:onError(err) print("An error occured while executing the query: " .. err) end if (result) then Msg("Player account created !\n") else Msg("Something went wrong with creating players stats !\n") end end   // Break \\   function player_exists1( ply )   steamID = ply:GetNWString("SteamID") local result = db:query("SELECT unique_id FROM ttt_kills WHERE unique_id = '"..steamID.."'") result:start() function result:onSuccess(data) print("KILL STATS: SUCCESS ON PLAYER ACCOUNT") local row = data[1] if(row ==nil) then new_player1( steamID, ply ) end end function result:onError(err) print("An error occured while executing the query: " .. err) end end // Break \\ function PlayerInitialSpawn1( ply )   timer.Create("Steam_id_delay1", 1, 1, function() SteamID = ply:SteamID() ply:SetNWString("SteamID", SteamID) player_exists1( ply )  db:ping() end)   end hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn1", PlayerInitialSpawn1 ) // Break \\ hook.Add( "PlayerDeath", "PlayersDiedDuringRoundKS", PlayerDiedDuringRoundStats ) function PlayerDiedDuringRoundStats(vic,inf,atter) db:ping() steamID = atter:GetNWString("SteamID") local query = db:query("UPDATE ttt_kills SET Kills = Kills+1 WHERE unique_id = '"..steamID.."'") query:start() if atter:GetRole() != ROLE_TRAITOR and vic:GetRole() != ROLE_TRAITOR then local query = db:query("UPDATE ttt_kills SET InnoInno = InnoInno+1 WHERE unique_id = '"..steamID.."'") query:start() elseif atter:GetRole() != ROLE_TRAITOR and vic:GetRole() == ROLE_TRAITOR then local query = db:query("UPDATE ttt_kills SET TerKills = TerKills+1 WHERE unique_id = '"..steamID.."'") query:start() elseif atter:GetRole() == ROLE_TRAITOR and vic:GetRole() != ROLE_TRAITOR then local query = db:query("UPDATE ttt_kills SET InnoKills = InnoKills+1 WHERE unique_id = '"..steamID.."'") query:start() end steamIDVic = vic:GetNWString("SteamID") local queryVic = db:query("UPDATE ttt_kills SET Deaths = Deaths+1 WHERE unique_id = '"..steamIDVic.."'") queryVic:start() end // Break \\ end
DB is probably nil at the time the game loads but isn't later on.
Sorry, you need to Log In to post a reply to this thread.