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

 net.Receive("spectatePlayer", function(length, client)
    if isSpectating then return end
        isSpectating = true
        playerLocal = client
        print(client:Nick() .. "Started spectating: " .. net.ReadEntity():Nick())


 hook.Add("Tick", "keydown", function()
        if not isSpectating then return end
                if (playerLocal:KeyDown(IN_FORWARD)) then
                        isSpectating = false
                        print(playerLocal:Nick().."Finished Spectating")

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

  • 6
    \$\begingroup\$ Please add in what the custom behavior is -- just saying that it's custom isn't very specific. \$\endgroup\$
    – anon
    Commented Jul 2, 2015 at 23:21

1 Answer 1


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

Add some whitespace in there.

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

Oh, and you should be keeping the cases (upper and lower) consistent in your messages:

"Started spectating: "
"Finished Spectating"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.