Wes Posted March 14, 2013 Share Posted March 14, 2013 Hy! I have a global chat resource, but I think it has wrong handler. You can only press B when the resource is restarted. So IF I'm online, and I restart it, it works fine. But when I log in, or someone logs in to the server, it doesnt work.. Here is the code: function globalMessage(thePlayer, cmd, ...) local message = table.concat ( { ... }, " " ); local name = getPlayerName(thePlayer); for _,v in ipairs(getElementsByType("player")) do outputChatBox("#FF0000[GLOBAL]#00FF00"..name..": #FFFFFF"..message,v, 255, 255, 255, true) end end addCommandHandler("global", globalMessage); addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), function() for index,player in pairs(getElementsByType("player")) do bindKey(player,"b", "down", "chatbox", "global"); end end ) I think it has something to do with the handlers, but I'm not really into these kind of stuff. Link to comment
TAPL Posted March 14, 2013 Share Posted March 14, 2013 Nah, doesn't work. Post your code then? Link to comment
iPrestege Posted March 14, 2013 Share Posted March 14, 2013 You can try this : function globalMessage(thePlayer, cmd, ...) local message = table.concat ( { ... }, " " ); local name = getPlayerName(thePlayer); for _,v in ipairs(getElementsByType("player")) do outputChatBox("#FF0000[GLOBAL]#00FF00"..name..": #FFFFFF"..message,v, 255, 255, 255, true) end end addCommandHandler("global", globalMessage); addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), function() for index,player in pairs(getElementsByType("player")) do bindKey(player,"b", "down", "chatbox", "global"); end end ) addEventHandler("onPlayerJoin",root, function () bindKey(source,"b", "down", "chatbox", "global"); end ); Please use Lua/Lua Tag -_-" Link to comment
Wes Posted March 14, 2013 Author Share Posted March 14, 2013 Thank you Mr.Pres[T]ege, you are da best! Link to comment
50p Posted March 14, 2013 Share Posted March 14, 2013 Can someone explain to me what's the point of looping through all players and sending the same message to all of them? Why not simply use root as the target (visibleTo)? outputChatBox("#FF0000[GLOBAL]#00FF00"..name..": #FFFFFF"..message, root, 255, 255, 255, true) Link to comment
iPrestege Posted March 14, 2013 Share Posted March 14, 2013 Can someone explain to me what's the point of looping through all players and sending the same message to all of them? Why not simply use root as the target (visibleTo)? outputChatBox("#FF0000[GLOBAL]#00FF00"..name..": #FFFFFF"..message, root, 255, 255, 255, true) I really do not know why, but always when I use Root other user's say it is wrong! Why?I do not know-_- Link to comment
50p Posted March 14, 2013 Share Posted March 14, 2013 Not all functions support it but if you read wiki you will see that default argument is getRootElement which is root. Even in the function description you can see the example showing the same thing as well as colour coding. Besides, getElementsByType is a slow function and if that is used very often then problems may occur. I am not sure if it has been changed to speed up the function but as far as I know, this function will loop through all elements created (by server or client) and get their type which may be slow if you have loads of objects, vehicles, players, markers, etc. Link to comment
iPrestege Posted March 14, 2013 Share Posted March 14, 2013 Not all functions support it but if you read wiki you will see that default argument is getRootElement which is root. Even in the function description you can see the example showing the same thing as well as colour coding. Thank you I will remember this always! Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now