General > Off-Topic

chat.AddText does not work right

(1/4) > >>

DH1806:
Hey Guys, I've used many hours for the following thing and I just can't imagine what the problem could be. It's about the chat.AddText function. It seems like it's changing values that are given to it, even if it is a string. But here's the code:

--- Code: ---function sendMessage(inumber, dnumber, x)
local inno = ""
local dete = ""
local lol = ""

inno = inumber
dete = dnumber
lol = x
if(CLIENT) then
chat.AddText(inno,dete,lol)
chat.AddText( color_white , "Diese Runde gibt es " , Color(0,235,0), inno , " Innocents, " , Color(0,0,235), dete, " Detectives", color_white, " und " ,  Color(255,7,0), lol, " Traitor.")
end
print ("i wrote "..lol)
end

--- End code ---

The function gets the values inumber (Number of Innocents), dnumber (Number of Detectives) and x (Number of Traitors). The values are right, I did a print() function so I could check it. I've made them to a string ( local XYZ = tostring(ICount)) so they can be written down with chat.AddText. The "print ("i wrote "..lol)" should check if the lol value was given right to the function. It always writes down the right values that were send in the console. The only thing which does not work is the chat.Add() function. It says lol would be 1, even if it's 2 and the print function after the chat.Add() also says that lol is 2. Same with the other values. I just don't understand why it seems that only the chat.Add() changes the values, even if it's a string it changes the numbers.
Does anyone has an idea why or how to fix it? :)

MrPresident:
which lua state are you calling sendMessage from?
Server or Client?

You can't just wrap if CLIENT then into a serverside call. For that, you'd need to send the values to the client using a net message and then use chat.AddText

DH1806:
Thanks for your answer. I wrote nothing there, but it should be serverside. It's a simple function in lua/autorun.

MrPresident:
http://wiki.garrysmod.com/page/chat/AddText

If the function is being called Serverside, then the if CLIENT bits won't be called correctly.
To do this properly what you should look into doing is sending a net message to the clients using net.Broadcast with the 3 variables you want to include in the chat.AddText then generating the chat message from a clientside net.Receive function.

http://wiki.garrysmod.com/page/net/Broadcast
http://wiki.garrysmod.com/page/net/Receive
http://wiki.garrysmod.com/page/net/WriteInt
http://wiki.garrysmod.com/page/net/ReadInt

Those links should get you started.

DH1806:
Thanks a lot, i will look into them later today.

Navigation

[0] Message Index

[#] Next page

Go to full version