Небольшой скрипт который сохраняет вашу профессию после пере захода на сервер.
Пример:
Взял профессию Мэра после чего вышел с сервера, через время зашёл на сервер и сетается профессия Мэра.
После смены профессии с Мэра на Гражданина при следующем входе будет выдаваться Гражданин
Скрипт подходит для серверов серьёзной отыгровки RP
local GAMEMODE = GAMEMODE or GM
local function init_tbl()
sql.Query("CREATE TABLE IF NOT EXISTS gr_saved_jobs ( SteamID TEXT, Job INTEGER )")
end
local function save_tbl(ply, job)
if ply:IsBot() then return end
if not RPExtraTeams[job] then return end
local data = sql.Query("SELECT * FROM gr_saved_jobs WHERE SteamID = " .. sql.SQLStr(ply:SteamID64()) .. ";")
if data and table.Count(data) > 0 then
sql.Query("UPDATE gr_saved_jobs SET Job = " .. job .. " WHERE SteamID = " .. sql.SQLStr(ply:SteamID64()) .. ";")
else
sql.Query("INSERT INTO gr_saved_jobs ( SteamID, Job ) VALUES( " .. sql.SQLStr(ply:SteamID64()) .. ", " .. job .. " )" )
end
end
local function getsavedjob(ply)
local val = sql.QueryValue( "SELECT Job FROM gr_saved_jobs WHERE SteamID = " .. sql.SQLStr(ply:SteamID64()) .. ";" )
if val ~= nil then
return tonumber(val)
else
return TEAM_CITIZEN
end
end
hook.Add("Initialize", "CPG_SaveJobTableCreate", function()
init_tbl()
end)
hook.Add("PlayerInitialSpawn", "CPG_GiveJobOnSpawn", function(ply)
if ply:IsBot() then return end
timer.Simple(0.5, function()
if IsValid(ply) then
local job = getsavedjob(ply)
if job ~= nil and job ~= TEAM_CITIZEN then
local max = RPExtraTeams[job].max
local numPlayers = team.NumPlayers(job)
if max ~= 0 and (max >= 1 and numPlayers >= max or max < 1 and (numPlayers + 1) / player.GetCount() > max) then
return
else
local setTeam = ply.changeTeam or ply.SetTeam
setTeam(ply, job, true)
end
else
save_tbl(ply, ply:Team())
end
end
end)
end)
hook.Add("OnPlayerChangedTeam", "CPG_SaveJob", function(ply, before, after)
if TEAM_CITIZEN ~= after then
save_tbl(ply, after)
end
end)
можно попробовать модифицировать, типо если игрок взял Мэра и случайно вылетел из сервера, чтобы был временной отчёт, типо через 5 минут профессия освобождается. Попробую щас сделать типо такого
ну а как тогда будет сохранятся данные о том, какая профессия у игрока. Сейчас я сделал типо такого, но пока безуспешно.
if timer.Exists("remove_savedjob_"..ply:UniqueID()) then
timer.Remove("remove_savedjob_"..ply:UniqueID())
end
end)
hook.Add("OnPlayerChangedTeam", "CPG_SaveJob", function(ply, before, after)
if TEAM_CITIZEN ~= after then
save_tbl(ply, after)
end
end)
hook.Add("PlayerDisconnected", "JobDisconnectremove", function(ply)
local id = ply:SteamID64()
timer.Create("remove_savedjob_"..ply:UniqueID(), 30, 1, function()
remove_tbl(id)
MsgN("Timer Stop")
end)
end)
local GAMEMODE = GAMEMODE or GM
local function init_tbl()
sql.Query("CREATE TABLE IF NOT EXISTS gr_saved_jobs ( SteamID TEXT, Job INTEGER )")
end
local function save_tbl(ply, job)
if ply:IsBot() then return end
if not RPExtraTeams[job] then return end
local data = sql.Query("SELECT * FROM gr_saved_jobs WHERE SteamID = " .. sql.SQLStr(ply:SteamID64()) .. ";")
if data and table.Count(data) > 0 then
sql.Query("UPDATE gr_saved_jobs SET Job = " .. job .. " WHERE SteamID = " .. sql.SQLStr(ply:SteamID64()) .. ";")
else
sql.Query("INSERT INTO gr_saved_jobs ( SteamID, Job ) VALUES( " .. sql.SQLStr(ply:SteamID64()) .. ", " .. job .. " )" )
end
end
local function remove_tbl(id)
local data = sql.Query("SELECT * FROM gr_saved_jobs WHERE SteamID = " .. sql.SQLStr(id) .. ";")
if data and table.Count(data) > 0 then
sql.Query("UPDATE gr_saved_jobs SET Job = " .. TEAM_CITIZEN .. " WHERE SteamID = " .. sql.SQLStr(id) .. ";")
else
sql.Query("INSERT INTO gr_saved_jobs ( SteamID, Job ) VALUES( " .. sql.SQLStr(id) .. ", " .. TEAM_CITIZEN .. " )" )
end
end
local function getsavedjob(ply)
local val = sql.QueryValue( "SELECT Job FROM gr_saved_jobs WHERE SteamID = " .. sql.SQLStr(ply:SteamID64()) .. ";" )
if val ~= nil then
return tonumber(val)
else
return TEAM_CITIZEN
end
end
hook.Add("Initialize", "CPG_SaveJobTableCreate", function()
init_tbl()
end)
hook.Add("PlayerInitialSpawn", "CPG_GiveJobOnSpawn", function(ply)
if ply:IsBot() then return end
timer.Simple(0.5, function()
if IsValid(ply) then
local job = getsavedjob(ply)
if job ~= nil and job ~= TEAM_CITIZEN then
local max = RPExtraTeams[job].max
local numPlayers = team.NumPlayers(job)
if max ~= 0 and (max >= 1 and numPlayers >= max or max < 1 and (numPlayers + 1) / player.GetCount() > max) then
return
else
local setTeam = ply.changeTeam or ply.SetTeam
setTeam(ply, job, true)
end
else
save_tbl(ply, ply:Team())
end
end
end)
if timer.Exists("remove_savedjob_"..ply:UniqueID()) then
timer.Remove("remove_savedjob_"..ply:UniqueID())
end
end)
hook.Add("OnPlayerChangedTeam", "CPG_SaveJob", function(ply, before, after)
if TEAM_CITIZEN ~= after then
save_tbl(ply, after)
end
end)
hook.Add("PlayerDisconnected", "JobDisconnectremove", function(ply)
local id = ply:SteamID64()
timer.Create("remove_savedjob_"..ply:UniqueID(), 30, 1, function()
remove_tbl(id)
MsgN("Timer Stop")
end)
end)