ID:891605
 
(See the best response by Nadrew.)
Code:

Login Screen
            UserLogin()
if(copytext(lowertext(key),1,7) == "telnet")
world.log << "(TELNET) [client.address] connected."
mb_msgout("\n[worldname]\nCoded by: ThunderZ\nLast Compile: [compiledate]")
login:
src.name=input("Name: ") as text
if(!name) del(src)
if(fexists("player_saves/[ckey(lowertext(name))]"))
world.log << "(TELNET) [client.address] - [name] FOUND"
var/savefile/F=new("player_saves/[ckey(lowertext(name))]")
var/CorrectPassword
F["Password"]>>CorrectPassword
F["key"]<<src.key
var/pass=input("Password: ") as password
if(pass==CorrectPassword)

NewMob(client, F)
else
mb_msgout("{YIncorrect password!{x")
del(src)
else
world.log << "(TELNET) [client.address] - [name] NOT FOUND"
if(Review_Name(name))
goto login
if(src.ConfirmName())
else
mb_msgout("{YPlease reconnect!{x")
del(src)
src.Password=input("\nEnter a password for your new character:\n") as password
if(src.PasswordCheck(Password))
mb_msgout("{YYour password must not start or end with a space nor can it be blank!{x")
del(src)
var/correctpass=input("\nConfirm your password please:\n") as password
if(correctpass==Password)
src.CharacterCreation()
else
mb_msgout("The two passwords did not match.")
del(src)
return .. ()


mob/Player Login()
mob
Player
Login()
Move(locate(last_x,last_y,last_z))
worldmobs += src
world.log << "(CHARACTER) [name] LOADED."
loggedIn = 1
//PROCEDURES//
if(Class == "Namekian") NamekRegeneration()
if(Class == "Saiyan") SSJTimer()
if(Class == "Human") KillSpiritBurstTimer()
if(Class == "Saiyan" || Class == "Human" || Class == "Namekian") KillKaiokenTimer()
if(Class == "Android") cansense = 0
UpdatePosition()
RegenStam()
CheckLinkDead()
StaminaDrain(src)
PrintEnergy()
Rest()
Sleep()
PrintTransformation(src)
Conscious()
Conscious2()
ResetCommands()
CheckFlight()
KillCombatTimer()
KillStunTimer()
Stunned()
LogPlayTime()
/////////////
ShowMap()
if(!invis && !linkdead) mb_msgout("{Y-->{x [ClassColor][name][Reset] entered [worldname]!", worldmobs)
if(linkdead)
mb_msgout("[name] reconnected.", oview(0))
linkdead = 0
mb_msgout(file2text("motd.txt"), src)
mb_msgout("\n{YPlaytime: [world.PlayTime(playtime)]{x", src)
mb_msgout("\n")
UpdatePrompt()
if(!Admin)
CheckLock()

Logout()
if(quit == 1)
if(!invis) mb_msgout("{Y-->{x [ClassColor][name][Reset] exited [worldname]!", worldmobs)
charging_ki = 0
InAction = 0
actionblocked = 0
stunned = 0
worldmobs -= src
loggedIn = 0
quit = 0
Save()
del(src)
else
mb_msgout("[name] went linkdead.", oview(0))
worldmobs -= src
linkdead = 1
Save()


NewMob proc
        NewMob(client/c, F as file)
var/mob/NewMob
var/mob/Oldmob = c.mob
var/MobPath = text2path("/mob/Player/[lowertext(F["Class"])]")
NewMob = new MobPath
NewMob.Read(F)
NewMob.cansave = 1
c.mob = NewMob
del(Oldmob)

Problem description:
Once in awhile when a player logs in the client will swap with the player that just logged in..

Could anyone please take a look at my code and maybe see why this is happening?

Best response
This is generally called "rolling back" it's caused by having a non-tmp variable that can be set to another atom (mob or otherwise) getting saved.

When the player loads it'll load that reference and force any players currently connected with a 'key' matching that saved reference to load into that mob.

I recommend reading this post on the matter.
So the problem is the built in saving and reading for byond it needs to be handled manually... ill try to figure this out its pretty detailed going to read over it a bit.

thanks.
The problem is setting non-tmp variables to values they shouldn't be set to and saving those values.

If you make the variables that can be set to other mobs all 'tmp' they won't get saved and the problem will be resolved.
oh so i see.. like the login where it has the variables for correct password etc they arent tmp so it is messing with the other mobs...
Nevermind thanks figured out more from that post + this http://www.byond.com/forum/?post=50892