Spectate Problems

When a player is spectated, they still have their weapons and player model. They also don’t spectate an entity with spectateEntity.

Post your code, how else are we meant to know what you’re doing wrong?

[/SUB]

ent:Spectate( OBS_MODE_CHASE )
ent:SpectateEntity( ply )

the ent is the the spectating player, and ply is the player that ent is going to be spectating.

Post the full code, we can’t tell what’s going on with just those two functions alone.

This is the function it’s in. It’s in the init.lua of a gamemode.

function GM:Think()
net.Receive( “RightClick”, function( length, ply )
local value = tonumber(net.ReadString())
if value == 0 then
–if ply:GetNWInt( “Cloaked” ) == 1 then
ply:SetNWInt( “Cloaked”, 0 )
ply:SetMaterial( “models/glass” )
ply:DrawShadow( true )
elseif value == 1 then
–elseif ply:GetNWInt( “Cloaked” ) == 0 then
ply:SetNWInt( “Cloaked”, 1 )
ply:SetMaterial( “sprites/heatwave” )
ply:DrawShadow( false )
end
end)
net.Receive( “E”, function( length, ply )
local tr = ply:GetEyeTrace()
local ent = tr.Entity
print(ply:GetNWInt( “Disguised” ))
if tr.Entity ~= nil then
–if IsValid(tr.Entity) then
local entX = tr.Entity:GetPos().x
local entY = tr.Entity:GetPos().y
local entZ = tr.Entity:GetPos().z

			local plyX = ply:GetPos().x
			local plyY = ply:GetPos().y
			local plyZ = ply:GetPos().z
			
			local dX = (entX-plyX)^2
			local dY = (entY-plyY)^2
			local dZ = (entZ-plyZ)^2
			local distance = math.sqrt( dX + dY + dZ )			
			if ply:GetNWInt( "Disguised" ) == 0 then
				if distance <= 120 and ply:GetNWInt( "Disguised" ) == 0 then
					if ent:IsPlayer() then
						if ply:GetNWInt( "Cloaked" ) == 0 then
							local PlyModel = ent:GetModel()
							local PlyPos = ent:GetPos()
							local PlyWeps = ent:GetWeapons() -- This is a table, get the stuff with a for loop
							
							ent:Spectate( OBS_MODE_CHASE )
							ent:SpectateEntity( ply )
							--ply:SetModel( PlyModel )
							--ply:SetPos( PlyPos )
	
							--ent:Spectate( OBS_MODE_ROAMING )
							--ent:SpectateEntity( ply )
							--[[for i,wep in pairs(PlyWeps) do
								ply:Give( wep )
							end]]
							ply:SetNWInt( "Disguised", 1 )
							ply:SetNWInt( "Cloaked", 0 )
						end
					elseif ent:GetClass() == "prop_physics" then
						ply:Spectate( OBS_MODE_CHASE )
						ply:SpectateEntity( ent )
						ply:SetNWInt( "SpectateEnt", ent )
						ply:SetNWInt( "Disguised", 1 )
						ply:SetNWInt( "Cloaked", 0 )
					end
				end
			elseif ply:GetNWInt( "Disguised" ) == 1 then
				if ent:GetClass() == "prop_physics" then
					ply:UnSpectate()
					ply:SetNWInt( "Cloaked", 1 )
					ply:SetNWInt( "Disguised", 0 )
					local plyPos = ply:GetPos()
					local HitPos = ply:GetEyeTrace().HitPos
					local eyePos = ply:EyePos()
					local eyeAng = ply:EyeAngles()
					ply:KillSilent()
					ply:Spawn()
					ply:SetPos( plyPos )
					ply:SetMaterial( "sprites/heatwave" )
					ply:DrawShadow( false )
					ply:SetEyeAngles( eyeAng )
					-- z x y
					--print(ToaX)
					--print(HitPos:Angle())
				elseif ent:IsPlayer() then
					ent:UnSpectate()
					ent:KillSilent()
					ent:Spawn()
					ply:SetNWInt( "Cloaked", 1 )
					ply:SetNWInt( "Disguised", 0 )
					local plyPos = ply:GetPos()
					local HitPos = ply:GetEyeTrace().HitPos
					local eyePos = ply:EyePos()
					local eyeAng = ply:EyeAngles()
					ply:KillSilent()
					ply:Spawn()
					ply:SetPos( plyPos )
					ply:SetMaterial( "sprites/heatwave" )
					ply:DrawShadow( false )
					ply:SetEyeAngles( eyeAng )
				end
			end
		--end
	end
end)
net.Receive( "Cloak", function( length, ply )
	ply:SetNWInt( "CloakPanel", net.ReadEntity() )
end)

end



function GM:Think()
	net.Receive( "RightClick", function( length, ply )
		local value = tonumber(net.ReadString())
		if value == 0 then
			--if ply:GetNWInt( "Cloaked" ) == 1 then
			ply:SetNWInt( "Cloaked", 0 )
			ply:SetMaterial( "models/glass" )
			ply:DrawShadow( true )
		elseif value == 1 then
			--elseif ply:GetNWInt( "Cloaked" ) == 0 then
			ply:SetNWInt( "Cloaked", 1 )
			ply:SetMaterial( "sprites/heatwave" )
			ply:DrawShadow( false )
		end
	end)
	net.Receive( "E", function( length, ply )
		local tr = ply:GetEyeTrace()
		local ent = tr.Entity
		print(ply:GetNWInt( "Disguised" ))
		if tr.Entity ~= nil then
			--if IsValid(tr.Entity) then
			local entX = tr.Entity:GetPos().x
			local entY = tr.Entity:GetPos().y
			local entZ = tr.Entity:GetPos().z

			local plyX = ply:GetPos().x
			local plyY = ply:GetPos().y
			local plyZ = ply:GetPos().z

			local dX = (entX-plyX)^2
			local dY = (entY-plyY)^2
			local dZ = (entZ-plyZ)^2
			local distance = math.sqrt( dX + dY + dZ )	
			if ply:GetNWInt( "Disguised" ) == 0 then
				if distance <= 120 and ply:GetNWInt( "Disguised" ) == 0 then
					if ent:IsPlayer() then
						if ply:GetNWInt( "Cloaked" ) == 0 then
							local PlyModel = ent:GetModel()
							local PlyPos = ent:GetPos()
							local PlyWeps = ent:GetWeapons() -- This is a table, get the stuff with a for loop

							ent:Spectate( OBS_MODE_CHASE )
							ent:SpectateEntity( ply )
							--ply:SetModel( PlyModel )
							--ply:SetPos( PlyPos )

							--ent:Spectate( OBS_MODE_ROAMING )
							--ent:SpectateEntity( ply )
							--[[for i,wep in pairs(PlyWeps) do
							ply:Give( wep )
							end]]
							ply:SetNWInt( "Disguised", 1 )
							ply:SetNWInt( "Cloaked", 0 )
						end
					elseif ent:GetClass() == "prop_physics" then
						ply:Spectate( OBS_MODE_CHASE )
						ply:SpectateEntity( ent )
						ply:SetNWInt( "SpectateEnt", ent )
						ply:SetNWInt( "Disguised", 1 )
						ply:SetNWInt( "Cloaked", 0 )
					end
				end
			elseif ply:GetNWInt( "Disguised" ) == 1 then
				if ent:GetClass() == "prop_physics" then
					ply:UnSpectate()
					ply:SetNWInt( "Cloaked", 1 )
					ply:SetNWInt( "Disguised", 0 )
					local plyPos = ply:GetPos()
					local HitPos = ply:GetEyeTrace().HitPos
					local eyePos = ply:EyePos()
					local eyeAng = ply:EyeAngles()
					ply:KillSilent()
					ply:Spawn()
					ply:SetPos( plyPos )
					ply:SetMaterial( "sprites/heatwave" )
					ply:DrawShadow( false )
					ply:SetEyeAngles( eyeAng )
					-- z x y
					--print(ToaX)
					--print(HitPos:Angle())
				elseif ent:IsPlayer() then
					ent:UnSpectate()
					ent:KillSilent()
					ent:Spawn()
					ply:SetNWInt( "Cloaked", 1 )
					ply:SetNWInt( "Disguised", 0 )
					local plyPos = ply:GetPos()
					local HitPos = ply:GetEyeTrace().HitPos
					local eyePos = ply:EyePos()
					local eyeAng = ply:EyeAngles()
					ply:KillSilent()
					ply:Spawn()
					ply:SetPos( plyPos )
					ply:SetMaterial( "sprites/heatwave" )
					ply:DrawShadow( false )
					ply:SetEyeAngles( eyeAng )
				end
			end
		end
	end)
	net.Receive( "Cloak", function( length, ply )
		ply:SetNWInt( "CloakPanel", net.ReadEntity() )
	end)
end


I tried to format this as best as possible, but HTML is messing with my indents
(This is the same code as above, just more readable)

You may either need to switch their team to TEAM_SPECTATOR / kill them, or in PreDrawPlayer return true; to not render them if they’re spectating.

Or, he could try using ply:StripWeapons() and ply:SetNoDraw(true).
Those two would solve his problems, without doing that.

Interesting. But when he leaves spectate mode he’d need to be given his weapons again.

I do this:

[lua]local PLAYER = FindMetaTable( “Player” );
function PLAYER:Invisible( )
// If we don’t have a variable defined to hold the status of invisibility, or the cache variable isn’t defined, or the cache has expired
if ( !self.__Invisible || !self.__InvisibleCache || CurTime( ) - self.__InvisibleCache > 1 ) then
// Update the invisibility status
self.__Invisible = self:GetNWInt( “Disguised” );

	// Update the cache
	self.__InvisibleCache = CurTime( );
end

// Return it
return self.__Invisible;

end

//
// Hook to see if player is rendered or not
// Returns: boolean preventDraw
function GM:PrePlayerDraw( Player )
if ( Player:Invisible( ) ) then
return true;
end

return false;

end[/lua]

Where PLAYER:Invisible is a custom function which returns true or false depending on whether or not they’re invisible. This hides weapons and the player. Instead of hard-coding it, it can be written as a hook:

[lua]// If added as a hook, respect other hooks which may call it, only return true; for logic which should hide the player.
hook.Add( “PrePlayerDraw”, “HideSpectatingPlayers”, function( Player )
if ( Player:Invisible( ) ) then
return true;
end
end );[/lua]

EDIT: Edited to include Invisible in your context; read this as to why I “cached” it: https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_nw_vars.lua

https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_hud_stuff.lua