Properly define a variable for each player?

Hello!

I am trying to create a system where VIPs can open a pointshop and select different colors for their name and chat text in the chat box. Currently, I have the system for coloring the name and text working fine in the client, but my issue comes with having separate colors appear for each player’s color choice.

In the client file, I have two variables set with their value being the color choice of the player who chatted. (Using playersay hook where ply is the player who talked)
ply.VIPNameColor
ply.VIPChatColor

And in the pointshop files, I have it set to define one of the two when it is equipped.
My problem is setting these variables server side for each player first so they can be modified by the pointshop purchase, and then be read in the client file that changes chat colors.
The best I have gotten to so far is located in sv_init of the addon:

[lua]hook.Add( “PlayerSpawn”, “VIPChat”, function VIPChat(ply)
if ply.VIPChatColor == nil then
ply.VIPChatColor = Color( 255, 255, 255 )
end
if ply.VIPNameColor == nil then
ply.VIPNameColor = Color( team.GetColor( ply:Team() ) )
end
end)[/lua]

This code is to check if the variables are already defined by pointshop and if they aren’t, then set them to default values.

Would I need to define the two variables in a shared file instead? And if so, am I on the right track?


hook.Add( "PlayerInitialSpawn", "VIPChat", function( ply )
    if not ply.VIPChatColor then
        ply.VIPChatColor = Color( 255, 255, 255 )
    end
    if not ply.VIPNameColor then
        ply.VIPNameColor = team.GetColor( ply:Team() )
    end
end)

Thanks, that solved a script breaking issue. However, the client still acts if the variable is nil and passing right over it, thus not coloring the chat. If I define the variables without ply. right in the client script itself, the colors appear just fine. This cannot work however as the colors need to be dynamic.

Is there a way to fix this? And do you need to see a script?

Are you accessing the variables serverside or clientside?

They are being accessed on a clientside script.

PlayerInitialSpawn is serverside only. You’ll need to use gameevent.Listen with player_connect to get it clientside: http://wiki.garrysmod.com/page/gameevent/Listen

Note that you will also have to move this to shared.

That worked. Thanks!