A lua function wont work until a lua refresh.

I have some server side code in my init.lua that works except for one line which does not error and wont work unless I refresh the lua after the initial load.



local cam = ents.FindByName("skycam")[1]

function GM:PlayerInitialSpawn( ply )
	ply:SetTeam( 1 ) -- Assign players to Team 1 (unauthenticated) until they log in and are authenticated into the appropriate team.
	ply:SetModel( "models/player/kleiner.mdl" ) -- Set a model otherwise players are just a black.... thing.
	umsg.Start("logintest")
	umsg.End()
	ply:SetViewEntity( cam )
end

function GM:PlayerSpawn( ply )
	ply:SetupHands()
	ply:Give("weapon_fists")
	ply:SetViewEntity( ply )
	ply:SetPos(Vector(-992,816,-2516))
	ply:SetEyeAngles(Angle(0,90,0))
end


The code ply:SetViewEntity doesn’t fire until a lua refresh. I need this to work flawlessly without a refresh. Any suggestions as to what is happening and why?

Maybe something is overriding the function?

PlayerInitialSpawn is called before the player entity is fully in-game. I’d recommend performing ALL camera modifications in the CalcView hook which controls the camera. You could use Team 1 as a test in CalcView to select which camera to use.

I like the way you think :smiley:

Something along the lines of if Team1 then the cam thing! Thanks~

Reopened:

What I have here works great but yet again doesn’t seem to want to work until a lua refresh despite being in a GM:Think hook :confused:



local cam = ents.FindByName("skycam")[1]
function GM:Think()
	for k, v in pairs( player.GetAll() ) do
		if v:Team(1) then
			v:SetViewEntity( cam )
		else
			v:SetViewEntity( v )
		end
	end
end


Are you using any form of base/framework and are editing the base?
Try to do it using hook.Add.

Trying a think hook that way also yields the refresh issue :confused:
I’m using base gamemode and am not re-writing or nulling any base hooks.

Do:

local cam

hook.Add(“InitPostEntity”, “FindCam”, function() cam = ents.FindByName(“skycam”)[1] end)

I’m actually starting to think something else is wrong here because the if statements in my code are not listening >:(

I re-wrote a lot of stuff and removed even more. After filtering everything down to just these two hooks I have this, but the refresh issue still persists and it seems that the If statements fire regardless of the variables checked :X

init.lua



local cam = ents.FindByName("skycam")[1]

function GM:PlayerInitialSpawn( ply )
	ply:SetTeam(1)
end

function GM:PlayerSpawn( ply )
	if ply:Team(2) then
		ply:SetViewEntity( ply )
		ply:SetModel( "models/player/kleiner.mdl" )
		ply:SetupHands()
		ply:SetPos(Vector(-992,816,-2516))
		ply:SetEyeAngles(Angle(0,90,0))
	else
		ply:SetViewEntity( cam )
		umsg.Start("logintest")
		umsg.End()
	end
end


And if Teams are an issue here

shared.lua



function GM:Initialize()
end

team.SetUp( 1, "unauthenticated", Color( 255, 255, 255 ) )
team.SetUp( 2, "authenticated", Color( 0, 0, 255 ) )


Seems that the IF statement in function GM:PlayerSpawn( ply ) runs regardless of the team i am on, which given this makes me think their is a bug somewhere or maybe I have just lost my mind…

Change ply:Team(2) to ply:Team()==2

Thanks wh1t3rabbit that fixed the IF (Did that change though?I used to always do ply:Team(#))

The refresh issue though is annoyingly persistent no matter how I set it up.

Also @wh1t3rabbit you are credited in the gamemode credits for having helped me yet again!

Thanks, if you want to add me on Steam I can try and help with the refresh issue.

Also :Team() has always worked the same as far as I know. It just returns the players team, it doesn’t do any comparing for you. So if a player is on team 4 and you do ply:Team(5), it is just going to return 4, not false like you’d be expecting.

The reason I see that your Script is not working is when cam is defined the map has not yet loaded, therefore the skycam entity does not exist yet. To fix this either:
A. Use the method Chessnut so nicely gave you(twice now)
B. Use the method Chessnut so nicely gave you(twice now)

Oh and the reason for it working on refresh is because when the gamemode refreshes, the map is created and your able to find your skycam ent.

Shush I didn’t get it till like 20 minutes ago, anyway Solved.