Corsaro Quad Posted November 19, 2011 Share Posted November 19, 2011 (edited) Hi. I'm doing a local chat but I can't use commands because when I type "/vehiclespawn" or "/login", it says normally "/vehiclespawn" and "/login" without execute the command. How can I resolve this? This is the code of the local chat script: -- Code deleted. Thanks for you support! Edited November 21, 2011 by Guest Link to comment
12p Posted November 19, 2011 Share Posted November 19, 2011 Could isPlayerInRangeOfPoint be replaced by isElementWithinColshape? Because you could seriously use colshapes, make your job easier Okay, let's check it. Use string.sub to check if there's "/" at position 1 in the string. Link to comment
Corsaro Quad Posted November 20, 2011 Author Share Posted November 20, 2011 Can you give me an example of the correct usage of string.sub, please? And, if you want, an example of isElementWithinColshape because I didn't find in the wiki and I don't know how it works. Thanks. Link to comment
unknooooown Posted November 20, 2011 Share Posted November 20, 2011 http://lua-users.org/wiki/StringLibraryTutorial string.sub(s, i [, j])s:sub(i [,j]) Return a substring of the string passed. The substring starts at i. If the third argument j is not given, the substring will end at the end of the string. If the third argument is given, the substring ends at and includes j. > = string.sub("Hello Lua user", 7) -- from character 7 until the end Lua user > = string.sub("Hello Lua user", 7, 9) -- from character 7 until and including 9 Lua > = string.sub("Hello Lua user", - -- 8 from the end until the end Lua user > = string.sub("Hello Lua user", -8, 9) -- 8 from the end until 9 from the start Lua > = string.sub("Hello Lua user", -8, -6) -- 8 from the end until 6 from the end Lua An example of string.sub that you can use: msg = "/test" testStringSub = string.sub(msg,1,1) print(testStringSub) This will return the "/" of msg. https://wiki.multitheftauto.com/wiki/IsE ... inColShape local circlearea = createColCircle ( 0, 0, 10 ) function ColShapeHit ( thePlayer, matchingDimension ) local detection = isElementWithinColShape ( thePlayer, circlearea ) --A variable called 'detection' stores the result of asking if the player --who entered a colshape is within the specific colshape called 'circlearea'. --The result is either true or false. if detection then outputChatBox ( getPlayerName(thePlayer).." is in the 'circle area' col shape" ) end --if detection was true then the player is in the col shape. Output a --message to confirm this end addEventHandler ( "onColShapeHit", getRootElement(), ColShapeHit ) Link to comment
Corsaro Quad Posted November 20, 2011 Author Share Posted November 20, 2011 Ok it works thanks. But now when I use a command with more arguments (example: /me blah blah blah or /pm Bob Yes I know! ) it doesn't work, it output normally the text. How can I resolve it? Thanks. Link to comment
unknooooown Posted November 20, 2011 Share Posted November 20, 2011 Ok it works thanks. But now when I use a command with more arguments (example: /me blah blah blah or /pm Bob Yes I know! ) it doesn't work, it output normally the text. How can I resolve it?Thanks. Hi again. Thats very simple to solve. function testFunc() local a = "/cmd name value" local cmd = gettok ( a, 1, 32 ) local name = gettok ( a, 2, 32 ) local value = gettok ( a, 3, 32 ) local cmd = string.sub(cmd,2) -- We use this line to remove the '/' from '/cmd'. outputChatBox("Cmd is: "..cmd) outputChatBox("Name is: "..name) outputChatBox("Value is: "..value) end addCommandHandler("result",testFunc) Let me explain what happens on the line: local cmd = gettok ( a, 1, 32 ) a = The string we want to split up, in this case, the string is "/cmd name value" 1 = The first word of that string 32 = The ASCII number representing the character you want to use to separate the word/string. In this case, we want to use SPACE to seperate our string, and space has the value '32' in ASCII. You can read about gettok here: https://wiki.multitheftauto.com/wiki/gettok And ASCII here: https://wiki.multitheftauto.com/wiki/ASCII The example will return: Cmd is: cmdName is: name Value is: value Hope that makes sense UPDATE: There is also https://wiki.multitheftauto.com/wiki/Split Works almost like gettok, but returns a table instead of a string. Look at the example in the link to understand it. But if you don't get, just ask in here Link to comment
Corsaro Quad Posted November 20, 2011 Author Share Posted November 20, 2011 (edited) Thanks man. Your explanation is very simple and the code now works fine. Thanks so much! EDIT: Oh... the server says the value var is a boolean value, but it outputs the text correctly... Server Error: "ERROR: resourcetest\localchat.lua:47: attempt to concatenate local 'value' (a boolean value)" This is the code: --Code deleted. Edited November 21, 2011 by Guest Link to comment
unknooooown Posted November 20, 2011 Share Posted November 20, 2011 Thanks man. Your explanation is very simple and the code now works fine. Thanks so much! EDIT: Oh... the server says the value var is a boolean value, but it outputs the text correctly... Server Error: "ERROR: resourcetest\localchat.lua:47: attempt to concatenate local 'value' (a boolean value)" This is the code: function onChat(player,_,...) cancelEvent() local px,py,pz=getElementPosition(player) local msg = table.concat({...}, " ") local nick=getPlayerName(player) local r,g,b = 255,255,255 local cmd = gettok(msg, 1, 32) local value = gettok(msg, 2, 32) for _,v in ipairs(getElementsByType("player")) do if msg == "/test" then testStringSub = string.sub(msg,1,1) outputChatBox("Test CMD!", source) elseif(cmd ~= "" and cmd ~= nil) then if(cmd == "/do" and value ~= "" and value ~= nil) then outputChatBox("value: " .. value, source) else outputChatBox("Error", source) end elseif isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then outputChatBox(nick.." says: #ffffff"..msg,v,r,g,b,true) end end end addCommandHandler("Chat",onChat) Found the problem. Change this line. for _,v in ipairs(getElementsByType("player")) do To this. for i,v in ipairs(getElementsByType("player")) do Just tested it.. Works with no errors Link to comment
Corsaro Quad Posted November 21, 2011 Author Share Posted November 21, 2011 (edited) The normal chat not works but the command works fine. I replaced the isPlayerInRangeOfPoint with isPlayerWithinColShape and the server give me an error... Error: Bad argument @ 'isElementWithinColShape' Code: --Code deleted. Edited November 21, 2011 by Guest Link to comment
12p Posted November 21, 2011 Share Posted November 21, 2011 Check comments. function onChat(player,_,...) cancelEvent() local circlearea = createColCircle ( 0, 0, 10 ) local detection = isElementWithinColShape ( player, circlearea ) --First argument was "thePlayer", not defined anywhere --local px,py,pz=getElementPosition(player) local msg = table.concat({...}, " ") local nick=getPlayerName(player) local r,g,b = 255,255,255 local cmd = gettok(msg, 1, 32) local value = gettok(msg, 2, 32) for i,v in ipairs(getElementsByType("player")) do if detection then if(cmd ~= "" and cmd ~= nil) then if(cmd == "/do" and value ~= "" and value ~= nil) then outputChatBox("value: " .. value, v) --You got wrong argument here, too. Fixed end else outputChatBox(nick.." says: #ffffff"..msg,v,r,g,b,true) end end end end addCommandHandler("Chat",onChat) addEventHandler ( "onColShapeHit", getRootElement(), onChat ) Link to comment
Corsaro Quad Posted November 21, 2011 Author Share Posted November 21, 2011 (edited) Fixed, but with isPlayerInRangeOfPoint. With isPlayerWithinColShape it doesn't work... Could you change from isPlayerInRangeOfPoint to isPlayerWithinColShape? Thanks. This is the code: --Code deleted. Edited November 21, 2011 by Guest Link to comment
12p Posted November 21, 2011 Share Posted November 21, 2011 Meh, you could do this, then. chat_range = 100 function onChat(player,_,...) cancelEvent() local circlearea = createColSphere ( 0, 0, chat_range ) local msg = table.concat({...}, " ") local r,g,b = 255,255,255 local cmd = gettok(msg, 1, 32) local value = gettok(msg, 2, 32) for i,v in ipairs(getElementsWithinColShape(circlearea, "player")) do if cmd ~= "" and cmd ~= nil then if cmd == "do" and value ~= "" and value ~= nil then outputChatBox("value: " .. value, v,r,g,b,true) else outputChatBox(getPlayerName(player).." says: #ffffff"..msg,v,r,g,b,true) end end end destroyElement ( circlearea ) end addCommandHandler("Chat",onChat) addEventHandler ( "onPlayerChat", getRootElement(), onChat ) Test it and tell me whether it works or not. Link to comment
Corsaro Quad Posted November 21, 2011 Author Share Posted November 21, 2011 It doesn't work... it doesn't output the command or the simple message and the server gives some errors: WARNING: Bad argument @ 'createColSphere' WARNING: Bad argument @ 'getElementsWithinColShape' ERROR: bad argument #1 to 'ipairs' (table expected, got boolean) WARNING: Bad argument @ 'createColSphere' WARNING: Bad argument @ 'getElementsWithinColShape' ERROR: bad argument #1 to 'ipairs' (table expected, got boolean) Link to comment
Castillo Posted November 21, 2011 Share Posted November 21, 2011 createColSphere requires 4 arguments, posX, posY, posZ, radius. Link to comment
Corsaro Quad Posted November 21, 2011 Author Share Posted November 21, 2011 createColSphere requires 4 arguments, posX, posY, posZ, radius. Thanks. I replaced it with createColCircle. Now it doesn't output the command or the simple message. How can I resolve it? (Remember: with isPlayerInRangeOfPoint it works fine!) Link to comment
Castillo Posted November 21, 2011 Share Posted November 21, 2011 chat_range = 100 function onChat(player,_,...) cancelEvent() local x, y = getElementPosition(player) local circlearea = createColSphere ( x, y, 5, chat_range ) local msg = table.concat({...}, " ") local r,g,b = 255,255,255 local cmd = gettok(msg, 1, 32) local value = gettok(msg, 2, 32) for i,v in ipairs(getElementsWithinColShape(circlearea, "player")) do if cmd ~= "" and cmd ~= nil then if cmd == "do" and value ~= "" and value ~= nil then outputChatBox("value: " .. value, v,r,g,b,true) else outputChatBox(getPlayerName(player).." says: #ffffff"..msg,v,r,g,b,true) end end end destroyElement ( circlearea ) end addCommandHandler("Chat",onChat) addEventHandler ( "onPlayerChat", getRootElement(), function (msg) onChat(source,_,msg) end) 1 Link to comment
Corsaro Quad Posted November 21, 2011 Author Share Posted November 21, 2011 (edited) Thanks man. It finally works! Thank you so much! /Solved Update: A little question, can I set this script as a Client-Side script? Edited November 21, 2011 by Guest Link to comment
Castillo Posted November 21, 2011 Share Posted November 21, 2011 (edited) You're welcome. P.S: Why hide the code? it may help someone else, don't be selfish. Edited November 21, 2011 by Guest Link to comment
Corsaro Quad Posted November 21, 2011 Author Share Posted November 21, 2011 You're welcome.P.S: Why hide the code? it may help someone else, don't be egoist. Ok. I try to recovery it... But a little question, can I set this script as a client-side script? Link to comment
Castillo Posted November 21, 2011 Share Posted November 21, 2011 Others won't see the messages then. Link to comment
Corsaro Quad Posted November 21, 2011 Author Share Posted November 21, 2011 Others won't see the messages then. Oh, ok. Thank you. Link to comment
Corsaro Quad Posted November 23, 2011 Author Share Posted November 23, 2011 (edited) Sorry for the double post but I have a problem with value var. In fact it reads only 1 token, but I want to read more tokens. Example: /do do some actions... It should outputs: * do some actions... I think to replace gettok to another function... This is the code: chat_range = 100 function onChat(player,_,...) cancelEvent() local x, y = getElementPosition(player) local circlearea = createColSphere ( x, y, 5, chat_range ) local msg = table.concat({...}, " ") local r,g,b = 255,255,255 local cmd = gettok(msg, 1, 32) local value = gettok(msg, 2, 32) playerName = getPlayerName(player) for i,v in ipairs(getElementsWithinColShape(circlearea, "player")) do if cmd ~= "" and cmd ~= nil then if cmd == "/do" and value ~= "" and value ~= nil then outputChatBox(value .. " (("..playerName.."))", v,r,g,b,true) else outputChatBox(playerName.." dice: #ffffff"..msg,v,r,g,b,true) end end end destroyElement ( circlearea ) end addCommandHandler("Chat",onChat) addEventHandler ( "onPlayerChat", getRootElement(), function (msg) onChat(source,_,msg) end) Edited November 23, 2011 by Guest Link to comment
12p Posted November 23, 2011 Share Posted November 23, 2011 I don't get what you mean. Instead of * do some actions... what does it output? PS. Don't care about double post if the first is at least 1 day late from the other. Link to comment
Corsaro Quad Posted November 23, 2011 Author Share Posted November 23, 2011 I don't get what you mean.Instead of * do some actions... what does it output? Example: /do some actions Output on the Chat: some actions I think the gettok of value var take only the second word when I type the command. But I want to take and output infinite words after cmd value. Link to comment
Castillo Posted November 23, 2011 Share Posted November 23, 2011 chat_range = 100 function onChat(player,_,...) cancelEvent() local x, y = getElementPosition(player) local circlearea = createColSphere ( x, y, 5, chat_range ) local msg = table.concat({...}, " ") local r,g,b = 255,255,255 local cmd = gettok(msg, 1, 32) playerName = getPlayerName(player) for i,v in ipairs(getElementsWithinColShape(circlearea, "player")) do if cmd ~= "" and cmd ~= nil then if cmd == "/do" and msg:gsub(cmd,"") ~= "" then outputChatBox(msg:gsub(cmd,"") .. " (("..playerName.."))", v,r,g,b,true) else outputChatBox(playerName.." dice: #ffffff"..msg,v,r,g,b,true) end end end destroyElement ( circlearea ) end addCommandHandler("Chat",onChat) addEventHandler ( "onPlayerChat", getRootElement(), function (msg) onChat(source,_,msg) end) That'll output the same, but it'll cut the /do part. 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