ENT:Think Problem

I currently have a ENT:Think function that repositions an entity when the owner dies, but when the owner dies the entity stays at the same spot. Here is the code:

function ENT:Think()
	local owner = self:GetOwner()
	if ( IsValid(owner) ) and owner:Alive() == false then
	for k,v in ipairs(ents.FindByClass("info_player_blue")) do
		if !IsValid(v) then continue end
		self:SetPos(v:GetPos() + Vector ( 40, 5, 0))
	return true

First of all, why are you doing it inside Think hook? It’s extremely unefficient, could be done in a non-meta function hooked to “PlayerDeath”.
Second of all:

if !IsValid(v) then continue end


Maybe he means return end?

But it’s pointless because v will always be valid in that case.

For you, PlayerDeath would be the right hook. So, when the player dies, it creates the entity and copies the bones; PlayerDeathThink is where you can limit respawning, and by using PlayerDeath you save yourself using a costly ents.Find* function in a Think hook…

Here’s a spectator system, using something like this will show you how to limit respawning if you’re going for that: https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/spectating_players_system.lua.html

Look at corpse.lua from TTT; it’ll show you how to copy the bones from the dead player to a ragdoll. You could freeze the ragdoll in place, or have it fall. Alternatively you could just spawn the entity there.

Additionally; instead of using ents.Find* each time a death occurs, search once and keep the results cached. If you’re not creating or removing them, this works great. Here’s a select-spawn system which shows how to “cache” a point entity: https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/playerselectspawn_system.lua.html

Why return within a search loop?

If its not a valid entity then skip to the next iteration in the loop.
That’s what the continue statement does, it skips to the next iteration.

If you put a return statement in there it would completely stop the loop and make it start over again, skipping over entities which could create bugs.

Exactly; That is why I was asking why he wanted him to change it to return instead of leaving it as continue.

Okay, changed it to:

function GM:PlayerDeath( victim, inflictor, attacker )
	entspawn = ents.FindByClass( 'info_player_blue' )[ 1 ]
	flagent = ents.FindByClass( 'ctf_flag' )[ 1 ]
	if ( victim == owner ) then
		flagent:SetOwner( entspawn )
		flagent:SetPos( entspawn )

But still doesn’t change the entity position or owner.

owner is undefined.
You are supposed to set position to a vector, not an entity.

[editline]24th May 2014[/editline]

Also, localize your variables.