Issue with Team being set after player is spawned (I think)

When I spawn on my server (Its always the initial spawn, any spawns after are fine) I never spawn in the right place (Im using team-based spawnpoints) and the player doesnt have a loadout. Any spawn after its fine (He spawns in the right place with the correct loadout)

Im using MySQL to find the players team from a database and then it selects a spawn based on his team, but the team is being set after he spawns so he doesnt spawn in the right place nor does he spawn with the correct loadout.

This is the parts of my code which effect it. (In order as they appear in init.lua)



function GM:PlayerLoadout( ply )
	ply:StripWeapons()
	ply:StripAmmo()
	print("Player has been given tools to destruct the earth :D")
	if ply:Team() == 1 then
		ply:Give( "weapon_hands" )
	end
	if ply:Team() == 2 then 
		ply:Give( "weapon_hands" )
	end
	if ply:Team() == 3 then
		ply:Give( "weapon_hands" )
		ply:Give( "weapon_smg1" )
		ply:Give( "weapon_shotgun" )
		ply:Give( "weapon_rp_shotgun" )
		ply:Give( "gmod_tool" )
	end
	
end

function connectToDatabase()
	databaseObject = mysqloo.connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT) -- The flour, yeast, salt, sugar and water required to make dough.
	databaseObject.onConnected = function() print("Database linked!") end -- Success!
	databaseObject.onConnectionFailed = function() print("Failed to connect to the database.") end -- FAILURE!
	databaseObject:connect()
end
connectToDatabase() -- Lets connect!

-- This is the snippet to add them to the database...were a bunch of stalkers.

function checkQueryJoin( query )
    local playerInfo = query:getData() -- Assigning the varible to the query dataah.
    if playerInfo[1] ~= nil then -- Lets check something!
	return true
    else
	return false
    end
end
 
function FirstJoinMysql( ply ) -- THIS WORKS.
    local query1 = databaseObject:query("SELECT * FROM users WHERE sid = '" .. ply:SteamID() .. "'") -- Check if they are in existance.
    query1.onSuccess = function(q) -- Check if it was successful...we dont want to fail.
	query1.onError = function(Q,E) print("Q1") print(E) end
        if not checkQueryJoin(q) then
	    local query2 = databaseObject:query("INSERT INTO users (sid, name, ip) VALUES ('" .. ply:SteamID() .. "', '" .. ply:Nick() .. "', '" .. ply:IPAddress() .. "')") -- LETS MAKE THE FUCKER!
	    query2.onSuccess = function(q)  print("Added client to the database") end -- Woop woop!
	    query2.onError = function(Q,E) print("Q1") print(E) end
	    query2:start() -- Lets start this shit dawg.	
        else
            print("Client is already in the database")-- FUCK YES NIGGA, YOUR ALREADY HERE!
        end
    end
    query1.onError = function(q,e) print("Something went wrong when checking") end -- Idiot.
    query1:start() -- Yay?
	local classquery1 = databaseObject:query("SELECT class FROM users WHERE sid = '" .. ply:SteamID() .. "'")
		classquery1.onData = function(Q,D) print("Data obtained!") 
			-- PrintTable(D) // Debug purposes
			if D['class'] == "civ" then
				print("User is a Civilian.")
				ply:SetTeam( 1 )
			elseif D['class'] == "res" then
				print("User is part of the resistence, good on you!")
				ply:SetTeam( 2 )
			elseif D['class'] == "cp" then
				print("User is part of civil protection.")
				ply:SetTeam( 3 )
			else
				print("We dont know the fucker.")
			end
		end
	classquery1:start()
end
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", FirstJoinMysql ) -- Make the pizza cook when the oven is turned on.

function GM:PlayerInitialSpawn ( ply , playerInfo )

	self.BaseClass:PlayerInitialSpawn( ply )
	
	if ply:IsAdmin() then
	else
	end
	
	

end

function GM:PlayerSpawn( ply )

	self.BaseClass:PlayerSpawn( ply )
	
	ply:SetGravity(1.20) -- Gravity doesnt exist in SPPPPPPPAAAACCCCCCCCCEEEEEEEEEEEEEE!
	ply:SetWalkSpeed(100) -- Walk fatboy, walk!
	ply:SetRunSpeed(200) -- Run fatboy, run!
	
end

function SpawnEnt()
    local ent1 = ents.Create("info_player_cp")
    ent1:SetPos(Vector( 2400, 502.17, 1100.59 ))
    ent1:Spawn()
    ent1:Activate()
	local ent2 = ents.Create("info_player_cp")
    ent2:SetPos(Vector( 1894.16, 531.71, 1030.88 ))
    ent2:Spawn()
    ent2:Activate()
	local ent3 = ents.Create("info_player_cp")
    ent3:SetPos(Vector( 1927.06, 766.66, 999.57 ))
    ent3:Spawn()
    ent3:Activate()
    local ent4 = ents.Create("info_player_cp")
    ent4:SetPos(Vector( 2272.19, 958.82, 989.19 ))
    ent4:Spawn()
    ent4:Activate()
    local ent5 = ents.Create("info_player_cp")
    ent5:SetPos(Vector( 2102.08, 821.53, 1019.59 ))
    ent5:Spawn()
    ent5:Activate()
    local ent6 = ents.Create("info_player_cp")
    ent6:SetPos(Vector( 2107.61, 555.95, 1021.38 ))
    ent6:Spawn()
    ent6:Activate()
	print( "Spawnpoints spawned" )
end
hook.Add("InitPostEntity", "MapSpawn", SpawnEnt)

function GM:PlayerSelectSpawn( ply )
    local spawns={}
    if  ply:Team() == 1 then
        spawns = ents.FindByClass( "info_player_start" )
    elseif ply:Team() == 2 then
        spawns = ents.FindByClass( "info_player_res" )
    elseif ply:Team() == 3 then
        spawns = ents.FindByClass( "info_player_cp" )
    else
        spawns = ents.FindByClass( "info_player_start" )
    end
    if(table.Count(spawns)>0)then return table.Random(spawns) end
	print( "Player Spawned" )
end


Sorry the code is so long.

[lua]function FirstJoinMysql( ply ) – THIS WORKS.
local query1 = databaseObject:query(“SELECT * FROM users WHERE sid = '” … ply:SteamID() … “’”) – Check if they are in existance.
query1.onSuccess = function(q) – Check if it was successful…we dont want to fail.
query1.onError = function(Q,E) print(“Q1”) print(E) end
if not checkQueryJoin(q) then
local query2 = databaseObject:query(“INSERT INTO users (sid, name, ip) VALUES (’” … ply:SteamID() … “’, '” … ply:Nick() … “’, '” … ply:IPAddress() … “’)”) – LETS MAKE THE FUCKER!
query2.onSuccess = function(q) print(“Added client to the database”) end – Woop woop!
query2.onError = function(Q,E) print(“Q1”) print(E) end
query2:start() – Lets start this shit dawg.
else
print(“Client is already in the database”)-- FUCK YES NIGGA, YOUR ALREADY HERE!
end
end
query1.onError = function(q,e) print(“Something went wrong when checking”) end – Idiot.
query1:start() – Yay?
local classquery1 = databaseObject:query(“SELECT class FROM users WHERE sid = '” … ply:SteamID() … “’”)
classquery1.onData = function(Q,D) print(“Data obtained!”)
– PrintTable(D) // Debug purposes
if D[‘class’] == “civ” then
print(“User is a Civilian.”)
ply:SetTeam( 1 )
elseif D[‘class’] == “res” then
print(“User is part of the resistence, good on you!”)
ply:SetTeam( 2 )
elseif D[‘class’] == “cp” then
print(“User is part of civil protection.”)
ply:SetTeam( 3 )
else
print(“We dont know the fucker.”)
end
ply:SetPos( gamemode.Call( “PlayerSelectSpawn”, ply ):GetPos() ) – Get their teams spawn point and move them there.
end
classquery1:start()
end
hook.Add( “PlayerInitialSpawn”, “PlayerInitialSpawn”, FirstJoinMysql ) – Make the pizza cook when the oven is turned on.[/lua]

Ok, thanks. That’s sorted the spawnpoint issue but not the loadout issue.

[lua]function FirstJoinMysql( ply ) – THIS WORKS.
local query1 = databaseObject:query(“SELECT * FROM users WHERE sid = '” … ply:SteamID() … “’”) – Check if they are in existance.
query1.onSuccess = function(q) – Check if it was successful…we dont want to fail.
query1.onError = function(Q,E) print(“Q1”) print(E) end
if not checkQueryJoin(q) then
local query2 = databaseObject:query(“INSERT INTO users (sid, name, ip) VALUES (’” … ply:SteamID() … “’, '” … ply:Nick() … “’, '” … ply:IPAddress() … “’)”) – LETS MAKE THE FUCKER!
query2.onSuccess = function(q) print(“Added client to the database”) end – Woop woop!
query2.onError = function(Q,E) print(“Q1”) print(E) end
query2:start() – Lets start this shit dawg.
else
print(“Client is already in the database”)-- FUCK YES NIGGA, YOUR ALREADY HERE!
end
end
query1.onError = function(q,e) print(“Something went wrong when checking”) end – Idiot.
query1:start() – Yay?
local classquery1 = databaseObject:query(“SELECT class FROM users WHERE sid = '” … ply:SteamID() … “’”)
classquery1.onData = function(Q,D) print(“Data obtained!”)
– PrintTable(D) // Debug purposes
if D[‘class’] == “civ” then
print(“User is a Civilian.”)
ply:SetTeam( 1 )
elseif D[‘class’] == “res” then
print(“User is part of the resistence, good on you!”)
ply:SetTeam( 2 )
elseif D[‘class’] == “cp” then
print(“User is part of civil protection.”)
ply:SetTeam( 3 )
else
print(“We dont know the fucker.”)
end
ply:SetPos( gamemode.Call( “PlayerSelectSpawn”, ply ):GetPos() ) – Get their teams
spawn point and move them there.
gamemode.Call( “PlayerLoadout”, ply )
end
classquery1:start()
end
hook.Add( “PlayerInitialSpawn”, “PlayerInitialSpawn”, FirstJoinMysql ) – Make the pizza cook when the oven is turned on.[/lua]

Try now.

That works perfectly, thank you :slight_smile: