Author Topic: GetRole() giving different results on Server and Client  (Read 163 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Hero Member
  • *****
  • Posts: 775
  • Karma: 50
GetRole() giving different results on Server and Client
« on: August 06, 2018, 03:48:29 PM »
If you're here for the solution: here it is.

Final Edit: Okay so I figured out the problem. I thought I was overwriting TTT's SendPlayerRoles method (by overwriting the method that calls this) but it turns out I wasn't. The method is this:

Code: Lua
  1. local function SendPlayerRoles()
  2.     for k, v in pairs(player.GetAll()) do
  3.         net.Start("TTT_Role")
  4.         net.WriteUInt(v:GetRole(), 2)
  5.         net.Send(v)
  6.     end
  7. end

and since my role numbers require more than 2 bits, I overwrote it to contain 5. Turns out it wasn't overwriting and still sending with 2 bits, which is not enough to send my numbers. Changing this function to
Code: Lua
  1. local function SendPlayerRoles()
  2.     for k, v in pairs(player.GetAll()) do
  3.         net.Start("TTT_Role")
  4.         net.WriteUInt(v:GetRole(), 5)
  5.         net.Send(v)
  6.     end
  7. end
makes everything work again.



I'm currently writing an addon that adds several custom roles to TTT, and I'm having an issue syncing the roles between Server/Client.

Right now, I have it so when they player joins, the server sends them a net message with all the current roles, and it is sending properly, I've checked:

Code: [Select]
0:
Alignment = Innocent
Color:
a = 200
b = 25
g = 200
r = 25
ID = 0
Limit = false
LimitNum = -1
Name = Innocent
Percent = -1
RequiredPlayers = -1
1:
Alignment = Traitor
Color:
a = 200
b = 25
g = 25
r = 200
ID = 1
Limit = false
LimitNum = -1
Name = Traitor
Percent = 0.15
RequiredPlayers = -1
2:
Alignment = Innocent
Color:
a = 200
b = 200
g = 25
r = 25
ID = 2
Limit = false
LimitNum = -1
Name = Detective
Percent = 0.1
RequiredPlayers = 6
3:
Alignment = None
Color:
a = 200
b = 180
g = 105
r = 255
ID = 3
Limit = true
LimitNum = 1
Name = Jester
Percent = -1
RequiredPlayers = 8
4:
Alignment = Innocent
Color:
a = 200
b = 250
g = 206
r = 135
ID = 4
Limit = false
LimitNum = -1
Name = Phoenix
Percent = 0.15
RequiredPlayers = 5
5:
Alignment = Innocent
Color:
a = 200
b = 255
g = 153
r = 204
ID = 5
Limit = false
LimitNum = -1
Name = Doctor
Percent = 0.13
RequiredPlayers = -1
6:
Alignment = Neutral
Color:
a = 200
b = 38
g = 118
r = 245
ID = 6
Limit = true
LimitNum = 1
Name = Arsonist
Percent = -1
RequiredPlayers = 10
Now, each role has their own ID, which is equivalent to ROLE_<role_name_upper>:

Code: Lua
  1. ROLE_INNOCENT = 0
  2. ROLE_TRAITOR = 1
  3. ROLE_DETECTIVE = 2
  4. ROLE_JESTER = 3
  5. ROLE_PHOENIX = 4
  6. ROLE_DOCTOR = 5
  7. ROLE_ARSONIST = 6
  8. ROLE_NONE = ROLE_INNOCENT
  9.  

However, I'm having issues with only one of these roles: the Jester:

Code: Lua
  1. 3:
  2.                 Alignment       =       None
  3.                 Color:
  4.                                 a       =       200
  5.                                 b       =       180
  6.                                 g       =       105
  7.                                 r       =       255
  8.                 ID      =       3
  9.                 Limit   =       true
  10.                 LimitNum        =       1
  11.                 Name    =       Jester
  12.                 Percent =       -1
  13.                 RequiredPlayers =       8
  14.  
As you can see, this role's ID is 3, because ROLE_JESTER = 3. However, when TTT is looking for the translation name of the role (which I have specified in english.lua), it says "Translation of nil not found".

When I did some digging, I found something strange. Basically, when TTT tries to put the translated role name there:

Code: Lua
  1. -- cl_hud.lua
  2. text = L[client:GetRoleStringRaw()]
  3.  
  4. -- player_ext_shd.lua
  5. local role_strings = {
  6.     [ROLE_TRAITOR] = "traitor",
  7.     [ROLE_INNOCENT] = "innocent",
  8.     [ROLE_DETECTIVE] = "detective",
  9.     [ROLE_JESTER] = "jester",
  10.     [ROLE_PHOENIX] = "phoenix",
  11.     [ROLE_DOCTOR] = "doctor",
  12.     [ROLE_ARSONIST] = "arsonist"
  13. }
  14.  
  15. function plymeta:GetRoleStringRaw()
  16.     return role_strings[self:GetRole()]
  17. end
  18.  

so I edited this a little, added this line:

Code: Lua
  1. function plymeta:GetRoleStringRaw()
  2.     MsgN(self:GetRole())
  3.  
  4.     return role_strings[self:GetRole()]
  5. end

And, when I'm Jester, it print's "7" over and over (in Yellow text, so on the Client), when it should be "3". I edited the role_strings to be this:

Code: Lua
  1. local role_strings = {
  2.     [ROLE_TRAITOR] = "traitor",
  3.     [ROLE_INNOCENT] = "innocent",
  4.     [ROLE_DETECTIVE] = "detective",
  5.     [ROLE_JESTER + 4] = "jester",
  6.     [ROLE_PHOENIX] = "phoenix",
  7.     [ROLE_DOCTOR] = "doctor",
  8.     [ROLE_ARSONIST] = "arsonist"
  9. }
  10.  
And things worked just fine. The only issue with this, though, is when I add a new role, their ID will be 7 and will mess with that.


Why is it that, on the server, ROLE_JESTER == 3, and on this part, client:GetRole() returns 7?

Also, I have overwritten the plymeta:GetRole() function to be this:

Code: Lua
  1. function plymeta:GetRole()
  2.     return self.role.ID
  3. end

so it SHOULD return 3 for Jester, but it doesn't. Can someone help me please?


TL;DR:

Code: Lua
  1. ] lua_run print(player.GetByID(1):GetRole())
  2. > print(player.GetByID(1):GetRole())...
  3. 3
  4. ] lua_run player.GetByID(1):SendLua("print(LocalPlayer():GetRole())")
  5. > player.GetByID(1):SendLua("print(LocalPlayer():GetRole())")...
  6. 7
  7.  
Why are those not the same?


EDIT: Okay so I think I've found my issue. I think my GetRole() and SetRole(role) functions aren't overwriting TTT's. Going to try to put those in an Initialize hook so they activate after TTT has loaded.

EDIT2: Just after I wrote that I figured that probably isn't the case because all the other roles work -_- I could really use some help here.

EDIT3: Just to clarify again, anything that checks if a person is a Jester only works if I check for GetRole() + 4.


Final Edit: Okay so I figured out the problem. I thought I was overwriting TTT's SendPlayerRoles method (by overwriting the method that calls this) but it turns out I wasn't. The method is this:

Code: Lua
  1. local function SendPlayerRoles()
  2.     for k, v in pairs(player.GetAll()) do
  3.         net.Start("TTT_Role")
  4.         net.WriteUInt(v:GetRole(), 2)
  5.         net.Send(v)
  6.     end
  7. end

and since my role numbers require more than 2 bits, I overwrote it to contain 5. Turns out it wasn't overwriting and still sending with 2 bits, which is not enough to send my numbers. Changing this function to
Code: Lua
  1. local function SendPlayerRoles()
  2.     for k, v in pairs(player.GetAll()) do
  3.         net.Start("TTT_Role")
  4.         net.WriteUInt(v:GetRole(), 5)
  5.         net.Send(v)
  6.     end
  7. end
makes everything work again.
« Last Edit: September 12, 2018, 11:34:28 AM by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me a PM or Join my Discord!.
--==[ Links ]==--
Steam
YouTube
Steam Group (feel free to join!)
Discord

Quote from: Steve Jobs
Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do. If you haven't found it yet, keep looking. Don't settle. As with all matters of the heart, you'll know when you find it. And, like any great relationship, it just gets better and better as the years roll on. So keep looking until you find it. Don't settle.