Look in a .txt File if User exists

Hey. I want, that a player that stands in the players.txt get to a extra Team. Team 3.
i tried that:
[lua]
function GM:PlayerConnect(ply,name)
if not file.Exists(“players.txt”) then file.Write(“players.txt”,"") end
local function FindByName(name) for _,v in ipairs(player.GetAll()) do
if string.match(v:Name(),name) then return v end
end end
print(FindByName(ply):Nick())
local function AddDonator(ply) file.Write(“donators.txt”,file.Read(“players.txt”)…FindByName(ply):Nick()…"
") end
end
[/lua]

and in the Player initial Spawn:
[lua]
for _,v in ipairs(string.Explode("
",file.Read(“players.txt”))) do
if ply:SteamID() == v then ply:SetTeam(3) end
end
[/lua]

Does not work. Please help :frowning:

[lua]local Player = FindMetaTable(“Player”)

function Player:LoadChars()
local FilePath = “path/to/file” … self:UniqueID()… “/players.txt”
if file.Exists(FilePath) then
print(“players loaded”)
local contents = util.KeyValuesToTable(file.Read(FilePath))
self.Chars = contents
datastream.StreamToClients(self,“chardata”,{Chars = contents})
else

end

end

function Player:SaveChars()
local FilePath = “path/to/file”…self:UniqueID()…"/players.txt"
if file.Exists(FilePath) then
print(“players saved”)
file.Write(FilePath,util.TableToKeyValues(self.Chars))
end
end[/lua]
try that

And how can i modify that script, that the Player that Stands on the list get to team 3 ?

Here you go :

[lua]local meta = FindMetaTable(“Player”)

function meta:LoadPlayerTeam()
if table.HasValue(util.KeyValuesToTable(file.Read(“players.txt”)),self:SteamID()) then
self:SetTeam(3)
end
end

function meta:SavePlayerTeam()
local tab = util.KeyValuesToTable(file.Read(“players.txt”))
if !table.HasValue(tab,self:SteamID()) then
table.insert(tab,self:SteamID())
file.Write(“player.txt”,util.TableToKeyValues(tab))
end
end[/lua]

hey.
i Wrote
[lua]
local Player = FindMetaTable(“Player”)

function Player:LoadChars()
local FilePath = “” … self:UniqueID()… “/players.txt”
if file.Exists(FilePath) then
print(“players loaded”)
local contents = util.KeyValuesToTable(file.Read(FilePath))
self.Chars = contents
datastream.StreamToClients(self,“chardata”,{Chars = contents})
else

end

end

function meta:LoadPlayerTeam()
if table.HasValue(util.KeyValuesToTable(file.Read(“players.txt”)),self:SteamID()) then
ply:SetTeam(3)
end
end
[/lua]

does not work :frowning:

Use KevKev’s, it should work.

OP code seems awfully familiar. :v:

The reason your initial code doesn’t work is because in your AddDonator code you were writing to donators.txt, not players.txt.

kevkes’s doesnt work too.

What is wrong with it?

I get:

advancedsandbox/gamemode/init.lua:33: attempt to index global ‘meta’ (a nil value)



********** COULDN’T LOAD GAMEMODE!! **********



There was a problem opening the gamemode file ‘advancedsandbox/gamemode/init.lua’

Registering gamemode ‘advancedsandbox’ derived from ‘sandbox’
ScriptEnforce is disabled
Loading Wire Tools
wire\stools\sv_display.lua:71: ‘)’ expected near ‘then’
MDLCache: Failed load of .VVD data for \hammy\pci_card.mdl
MDLCache: Failed load of .VVD data for \

[lua]FILE_PATH = “players.txt”
TEAM_NUMBER = 3

// SETS THE PLAYER’S TEAM ON CONNECTION
function GM:PlayerInitialSpawn(pl)
if file.Exists(FILE_PATH) then
if table.HasValue(string.Explode("
", pl:SteamID())) then
pl:SetTeam(TEAM_NUMBER)
else
// MAKES THE PLAYER JOIN TEAM 1 IF NOT IN PLAYERS.TXT FILE
pl:SetTeam(1)
end
else
file.Write(FILE_PATH, “”)
end
end

// ADDS THE PLAYER TO PLAYER.TXT
function AddPlayer(pl, cmd, args)
ply = args[1]
for k, v in pairs(player.GetAll()) do
if string.find(v:Nick(), ply) || string.find(v:Name(), ply) then
file.Write(FILE_PATH, v:SteamID() … "
“)
if v:Alive() then
pl:SetTeam(TEAM_NUMBER)
pl:PrintMessage(HUD_PRINTTALK, ply … " has been added to players.txt”)
end
end
end
concommand.Add(“AddPlayer”, AddPlayer)[/lua]

I didn’t check for any syntax errors. Tell me if there’s anything wrong with it and I’ll fix it.

To add players to the file from console, type this:


AddPlayer "name"

For example:


AddPlayer "ColtoM Lulz Wat"

Hey. Yes there were 2 Problems but i fixed it myself.
Thank you it works very good.
But i have to say. The code i posted was Wrong. I Pasted the False :smiley:
Nevermind.
I think it does not Work :frowning: My Full script is naw:
[lua]
function GM:PlayerSpawn(ply)

self.BaseClass:PlayerSpawn( ply )   
ply:SetGravity( 0.75 ) 

ply:SetMaxHealth(150,true)
ply:SetWalkSpeed(300)
if file.Exists(FILE_PATH) then
if table.HasValue(string.Explode("
", ply:SteamID())) then
ply:SetTeam(3)
else
// MAKES THE PLAYER JOIN TEAM 1 IF NOT IN PLAYERS.TXT FILE
ply:SetTeam(2)
end
else
file.Write(FILE_PATH, “”)
end
if ply:IsAdmin() then ply:SetTeam(1) end
if ply:IsAdmin() then
ply:SetWalkSpeed(AdminWSpeed)
ply:SetRunSpeed (AdminRSpeed)
ply:GodEnable()
else
ply:SetWalkSpeed(GuestWSpeed)
ply:SetRunSpeed (GuestRSpeed)
end
end
[/lua]

The Player who joins always get to team 2 but i entered his name

/edit.
Addplayer does not work. if im playin with 2 players and i enter my name, The Steamid of playerid 1 will be entered

[lua]FILE_PATH = “players.txt”
TEAM_NUMBER = “3”

function GM:PlayerSpawn(ply)

self.BaseClass:PlayerSpawn( ply )   
ply:SetGravity( 0.75 ) 
ply:SetMaxHealth(150,true)
ply:SetWalkSpeed(300)	

if file.Exists(FILE_PATH) then
    if table.HasValue(string.Explode("

", ply:SteamID())) then
ply:SetTeam(TEAM_NUMBER)
else
ply:SetTeam(2)
end
else
file.Write(FILE_PATH, “”)
end

if ply:IsAdmin() then
ply:SetTeam(1)
ply:SetWalkSpeed(AdminWSpeed)
ply:SetRunSpeed (AdminRSpeed)
ply:GodEnable()
else
ply:SetWalkSpeed(GuestWSpeed)
ply:SetRunSpeed (GuestRSpeed)
end
end

function AddPlayer(pl, cmd, args)
ply = args[1]
for k, v in pairs(player.GetAll()) do
if string.find(v:Nick(), ply) || string.find(v:Name(), ply) then
file.Write(FILE_PATH, v:SteamID() … "
“)
if v:Alive() then
pl:SetTeam(TEAM_NUMBER)
pl:PrintMessage(HUD_PRINTTALK, ply … " has been added to players.txt”)
end
end
end
concommand.Add(“AddPlayer”, AddPlayer)[/lua]

/edit. Thankyou
it works!
/edit again.
addplayer does not work twice.
i can only add 1 person

[lua]FILE_PATH = “players.txt”
TEAM_NUMBER = “3”

function GM:PlayerSpawn(ply)

self.BaseClass:PlayerSpawn( ply )   
ply:SetGravity( 0.75 ) 
ply:SetMaxHealth(150,true)
ply:SetWalkSpeed(300)	

if file.Exists(FILE_PATH) then
    if table.HasValue(string.Explode("

", ply:SteamID())) then
ply:SetTeam(TEAM_NUMBER)
else
ply:SetTeam(2)
end
else
file.Write(FILE_PATH, “”)
end

if ply:IsAdmin() then
ply:SetTeam(1)
ply:SetWalkSpeed(AdminWSpeed)
ply:SetRunSpeed (AdminRSpeed)
ply:GodEnable()
else
ply:SetWalkSpeed(GuestWSpeed)
ply:SetRunSpeed (GuestRSpeed)
end
end

function AddPlayer(pl, cmd, args)
ply = args[1]
for k, v in pairs(player.GetAll()) do
if string.find(v:Nick(), ply) || string.find(v:Name(), ply) then
file.Write(FILE_PATH, file.Read(FILE_PATH)… “” …v:SteamID() … "
“)
if v:Alive() then
pl:SetTeam(TEAM_NUMBER)
pl:PrintMessage(HUD_PRINTTALK, ply … " has been added to players.txt”)
end
end
end
concommand.Add(“AddPlayer”, AddPlayer)[/lua]

That was because it was overwriting the file. I fixed it.

Nope its Overwriting again

/eidt wait wait wait. didnt reload te gamemode

okay the Playeradd works.
and if i add my name i get To the right team. But other user not :frowning:
/edit. After restart noone gets to the right team
maybe can you join my server? then ill show you that

It should work, if not then I dont have a clue. Sorry.

oh okay.
:frowning:

[editline]07:58PM[/editline]

I get to the right team.
But noone other.

your filepaths seem odd to me. a file path should be something like
gamemode/players.txt not just players.txt should it not?

Nop.
Its shared in the data folder