DTP DarkRP Team Persistence
DTP
Dark RP Team Persistence
See Our DTP Code Here
In Addition File Structure is dtp/lua/autorun
In Addition File Structure is dtp/lua/autorun/client/cl_remember_team.lua
In Addition File Structure is dtp/lua/autorun/server/sv_remember_team.lua
Code as Follows Starting Of As cl_remember_team.lua
-- Function to notify the player that their team has been restored
net.Receive("NotifyTeamRestored", function()
local restoredTeam = net.ReadString()
-- Display a message to the player in the chat
chat.AddText(Color(0, 255, 0), "[DarkRP] ", Color(255, 255, 255), "You have been placed back into your previous team: " .. restoredTeam)
end)
print("Server Client Code Loaded For SaveTeam!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
Server Side Code sv_remember_team.lua
-- File path: lua/autorun/server/sv_remember_team.lua
-- Function to print ASCII art to console on server startup
local function PrintAsciiArt()
print([[
DDDD TTTTT PPPP
D D T P P
D D T PPPP
D D T P
DDDD T P
]])
print("[DarkRP] Team Persistence Addon Loaded (DTP)")
end
-- Ensure the database table exists on server startup
local function EnsureDatabaseExists()
if not sql.TableExists("darkrp_team_memory") then
local createTableQuery = [[
CREATE TABLE IF NOT EXISTS darkrp_team_memory (
steamid TEXT PRIMARY KEY,
team INTEGER
)
]]
local result = sql.Query(createTableQuery)
if result == false then
print("SQL Error while creating table: " .. sql.LastError())
else
print("[DarkRP] Team memory table created successfully!")
end
else
print("[DarkRP] Team memory table already exists.")
end
end
-- Call the function when the server starts
hook.Add("Initialize", "EnsureTeamMemoryDatabase", function()
PrintAsciiArt() -- Print ASCII art during server startup
EnsureDatabaseExists()
end)
-- Function to save player's team when they disconnect or change teams
local function SavePlayerTeam(ply)
if not IsValid(ply) then return end
local steamID = ply:SteamID()
local teamID = ply:Team()
-- Store the team in the SQL database, overwriting the previous entry
local query = "REPLACE INTO darkrp_team_memory (steamid, team) VALUES (" .. sql.SQLStr(steamID) .. ", " .. teamID .. ")"
local result = sql.Query(query)
if result == false then
print("SQL Error while saving player team: " .. sql.LastError())
else
print("Saved team for player " .. ply:Nick() .. " (SteamID: " .. steamID .. ")")
end
end
-- Hook to save the team on player disconnect
hook.Add("PlayerDisconnected", "SaveTeamOnDisconnect", function(ply)
SavePlayerTeam(ply)
end)
-- Function to restore player's team when they join or revert to default
local function RestorePlayerTeam(ply)
local steamID = ply:SteamID()
-- Retrieve the team from the SQL database
local query = "SELECT team FROM darkrp_team_memory WHERE steamid = " .. sql.SQLStr(steamID)
local result = sql.QueryRow(query)
-- Default team (e.g., TEAM_CITIZEN) if no previous team is found
local defaultTeam = TEAM_CITIZEN
if result and result.team then
local teamID = tonumber(result.team)
-- Check if the team is valid
if team.Valid(teamID) then
-- Set the player's team to the one from the database
ply:changeTeam(teamID, true) -- The `true` bypasses checks for joining restrictions change to false to stop the bypass
-- Notify the player about team restoration
net.Start("NotifyTeamRestored")
net.WriteString(team.GetName(teamID)) -- Send the team name to the client
net.Send(ply)
print("Restored team for player " .. ply:Nick() .. " (SteamID: " .. steamID .. ") to team " .. team.GetName(teamID))
else
-- If the team is not valid, fall back to default team
ply:changeTeam(defaultTeam, true)
print("Invalid team found for player " .. ply:Nick() .. ", reverted to default team.")
end
else
-- If no previous team found, assign to default team
ply:changeTeam(defaultTeam, true)
print("No previous team found for player " .. ply:Nick() .. ", assigned to default team.")
end
end
-- Hook to restore the team on player join
hook.Add("PlayerInitialSpawn", "RestoreTeamOnJoin", function(ply)
-- Delay a bit to ensure everything is loaded properly
timer.Simple(1, function()
if IsValid(ply) then
RestorePlayerTeam(ply)
end
end)
end)
-- Hook to delete previous team entry when the player changes teams
hook.Add("OnPlayerChangedTeam", "RemoveOldTeamEntry", function(ply, oldTeam, newTeam)
-- Check if the new team is different from the old one
if oldTeam ~= newTeam then
local steamID = ply:SteamID()
-- Delete the old team entry from the database
local query = "DELETE FROM darkrp_team_memory WHERE steamid = " .. sql.SQLStr(steamID)
local result = sql.Query(query)
if result == false then
print("SQL Error while deleting old team entry: " .. sql.LastError())
else
print("Deleted old team entry for player " .. ply:Nick() .. " (SteamID: " .. steamID .. ")")
end
-- Save the new team
SavePlayerTeam(ply)
end
end)
util.AddNetworkString("NotifyTeamRestored") -- Register the network message
Or Click Me For The ZIP
https://steamcommunity.com/sharedfiles/filedetails/?id=3338646339 STEAM Link