Jump to content

[Solved]Issues with custom chat


Corsaro Quad

Recommended Posts

Posted (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 by Guest
Posted

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.

Posted

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.

Posted

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) -- 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 ) 

Posted

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.

Posted
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: cmd

Name 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 :-)

Posted (edited)

Thanks man. Your explanation is very simple and the code now works fine. Thanks so much! :D

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 by Guest
Posted
Thanks man. Your explanation is very simple and the code now works fine. Thanks so much! :D

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 :-)

Posted (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 by Guest
Posted

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 ) 

Posted (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 by Guest
Posted

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.

Posted

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) 

Posted
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!)

Posted
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) 

  • Thanks 1
Posted
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?

Posted (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 by Guest
Posted

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.

Posted
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.

Posted
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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...