Garry's Mod custom spectate player

I have been making my own scoreboard, and needed a custom spectate function for it, so I made this:

  util.AddNetworkString( "spectatePlayer" )
local playerLocal
local isSpectating = false

if isSpectating then return end
isSpectating = true
playerLocal = client
print(client:Nick() .. "Started spectating: " .. net.ReadEntity():Nick())
client:Spectate(5)

end)

if not isSpectating then return end
if (playerLocal:KeyDown(IN_FORWARD)) then
isSpectating = false
playerLocal:UnSpectate()
print(playerLocal:Nick().."Finished Spectating")
end
end)


Is there anything I could improve on? Am I over-complicating anything?

• Please add in what the custom behavior is -- just saying that it's custom isn't very specific. – Fund Monica's Lawsuit Jul 2 '15 at 23:21

There's a few things to comment on:

I'm not a Gmod developer, so I'm not familiar with the spectating abilities built in, but, I'd assume that if any exist, they're probably better to use than re-inventing the wheel.

There's only two states: not spectating, and spectating. What happens if I want to simply switch between players? Do I have to stop spectating, and then select a new player to spectate?

Why can I not just press lmb and go to the next?

util.AddNetworkString( "spectatePlayer" )


You've got extraneous whitespace in your brackets.

print(client:Nick() .. "Started spectating: " .. net.ReadEntity():Nick())
print(playerLocal:Nick().."Finished Spectating")


String concatenation joins the strings, as they are. Meaning, it would read like:

QuillStarted spectating: James Heald.
QuillFinished Spectating

print(client:Nick() .. " Started spectating: " .. net.ReadEntity():Nick())

"Started spectating: "