Spectate Fail

Hey guys,
I’m spectating a player using some code. When I spectate with this code :

[lua]Pl:SetMoveType(MOVETYPE_OBSERVER)
Pl:Spectate(OBS_MODE_IN_EYE)
Pl:SpectateEntity(targ)
Pl:Lock()[/lua]

It spectates the correct player, but the person who is spectating’s gun is shown floating in the air. The Pl:Lock() is necessary or else the player will be able to run around while spectating someone, even though I set the movetype to OBSERVER.

This is not the biggest problem though. When I run the unspectate code:

[lua]Pl:Spectate(OBS_MODE_NONE)
Pl:UnSpectate()
Pl:SetMoveType(MOVETYPE_WALK)
Pl:UnLock()[/lua]

The player unspectates, but he has no model and cannot be seen, shot, or collided with by other players when they run into him, yet he can shoot, and he can’t run through players on his own, but you can still see his gun floating, and his HUD doesn’t show up on his screen.

Is spectate just another one of garry’s casualties or am I doing something wrong?

Perhaps to sort out the guns being seen in spectator mode, you could use pl:StripWeapons(). Then in the unspectate mode use a GM:PlayerLoadout hook?

The player acquires weapons throughout the game, and if they spectate, they shouldn’t lose the guns they’ve gotten.

You don’t need to lock the player as it should automatically prevent them from moving depending on what spectate mode you’re using (and as long as targ is a valid entity). Also, you would need to strip their weapons (just save it and give it back to em later).

I’m not sure about the whole after-effects of spectating problem, but you could try using any of these functions:


ply:PhysWake()
ply:SetSolid(SOLID_VPHYSICS)
ply:SetCollisionGroup(COLLISION_GROUP_PLAYER)


If worst comes to worst, you can just kill them after unspectating.

[lua]
ply:GetViewModel():SetColor(Color(0,0,0,0))
[/lua]

Spectate



ply:SetNWInt("spos",ply:GetPos())
ply:SetNWInt("sang",ply:GetAngles())
ply:Spectate( OBS_MODE_IN_EYE )
ply:SpectateEntity( target )
ply:StripWeapons()


Unspectate



ply:UnSpectate()
ply:Spawn()
timer.Simple( 0.1, function()
ply:SetPos( ply:GetNWInt("spos") )
ply:SetAngles( Vector(ply:GetNWInt("sang")) )
end )