Jump to content

Proximity Chat/Text commands


simstosh

Recommended Posts

Hi.

I'm working on a Zombie Roleplay script (isn't complex like a default roleplay server, but a "basic" version of him, only some commands/gameplay style like) to put on my server. I search about the proximity chat on wiki and i got one on onPlayerChat section. The chat (without commands) works normally... I've tried to do the same thing to the commands, but he gave me only errors on console, like:

[2010-08-11 01:12:30] WARNING: zmrp\zmrp.lua:224: Bad argument @ 'getElementPosition'
[2010-08-11 01:12:30] WARNING: zmrp\zmrp.lua:225: Bad argument @ 'createColSphere'
[2010-08-11 01:12:30] WARNING: zmrp\zmrp.lua:226: Bad argument @ 'getElementsWithinColShape'
[2010-08-11 01:12:30] WARNING: zmrp\zmrp.lua:227: Bad argument @ 'getPlayerName'
[2010-08-11 01:12:30] WARNING: zmrp\zmrp.lua:228: Bad argument @ 'destroyElement'
[2010-08-11 01:12:30] ERROR: zmrp\zmrp.lua:230: bad argument #1 to 'ipairs' (table expected, got boolean)

This errors shows whenever I use /me text or /do text.

Here is the complete chat lines:

function sendMessageToNearbyPlayers( message, messageType )
if messageType == 0 then
local posX, posY, posZ = getElementPosition( source )
local chatSphere = createColSphere( posX, posY, posZ, chatRadius )
local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" )
local playerNameIs = getPlayerName (source)
destroyElement( chatSphere )
 
for index, nearbyPlayer in ipairs( nearbyPlayers ) do
outputChatBox( playerNameIs.." diz: "..message, nearbyPlayer )
end
end
end
addEventHandler( "onPlayerChat", getRootElement(), sendMessageToNearbyPlayers )
 
function TalkMe (player, commandName, message )
if(message == "") then
	OutputChatBox ( "USO: /me [ação]", player, 255, 255, 0 )
end
local posX, posY, posZ = getElementPosition( source )
local chatSphere = createColSphere( posX, posY, posZ, chatRadius )
local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" )
local playerNameIs = getPlayerName (source)
destroyElement( chatSphere )
 
for index, nearbyPlayer in ipairs( nearbyPlayers ) do
outputChatBox ( playerNameIs.." "..message, nearbyPlayer, 238, 18, 147 )
end
end
addCommandHandler ( "me", TalkMe )
 
function TalkDo (player, commandName, message)
if(message == "") then
	OutputChatBox ( "USO: /do [ação]", player, 255, 255, 0 )
end
local posX, posY, posZ = getElementPosition( source )
local chatSphere = createColSphere( posX, posY, posZ, chatRadius )
local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" )
local playerNameIs = getPlayerName (source)
destroyElement( chatSphere )
 
for index, nearbyPlayer in ipairs( nearbyPlayers ) do
outputChatBox ( message.." (("..playerNameIs.."))", nearbyPlayer, 238, 18, 147 )
end
end
addCommandHandler ( "do", TalkDo )
 
function blockChatMessage()
cancelEvent()
end
addEventHandler( "onPlayerChat", getRootElement(), blockChatMessage )

And how can I force to show a message, like "Correct Usage: /me [action]" if the player only type /me or /do?

Thanks for all

Link to comment

[2010-08-11 01:12:30] WARNING: zmrp\zmrp.lua:224: Bad argument @ 'getElementPosition'
[2010-08-11 01:12:30] WARNING: zmrp\zmrp.lua:225: Bad argument @ 'createColSphere'
[2010-08-11 01:12:30] WARNING: zmrp\zmrp.lua:226: Bad argument @ 'getElementsWithinColShape'
[2010-08-11 01:12:30] WARNING: zmrp\zmrp.lua:227: Bad argument @ 'getPlayerName'
[2010-08-11 01:12:30] WARNING: zmrp\zmrp.lua:228: Bad argument @ 'destroyElement'
[2010-08-11 01:12:30] ERROR: zmrp\zmrp.lua:230: bad argument #1 to 'ipairs' (table expected, got boolean)

This means you Argument, from "getElementPosition" to "DestroyElement" are wrongs, i think its thePlayer not source.

but idk whats wrong whit "ipairs" error... :roll:

Link to comment

No, you didn't understand...

The normal chat works perfectly. the ChatRadius stays at the start of the script (local chatRadius = 20).

I just copied the functions from the "sendMessageToNearbyPlayers" and put on the commands. The chat works normal, but the commands not (Chat doesn't return any error, but the commands yes, and the base stays at the same)... Understand now?

Link to comment

Well, I don't know why, but the chat and the /do works normally. I do other commands (/o & /ooc, /b, and the /me), but I can't put this working...

The /me doesn't work. I think it's because MTA already has this function on onPlayerChat (messageType = 1). I've used him on sendMessageToNearbyPlayers function, but I want to put "Correct Usage: /me [text]" on him, to the player who doesn't know the function usage. If stays at this function, I can't put to show the message.

The (/o)oc and the /b, if I use only the command, he returns "Uso correto: /b [texto]" or something like that, and if I put the text after the command, nothing returns on game, buton on console shows this:

[17:24:24] ERROR: zmrp\zmrp.lua:217: attempt to call local 'message' (a string value) >> refeer to (/o)oc

[17:24:28] ERROR: zmrp\zmrp.lua:227: attempt to call local 'message' (a string value) >> refeer to /b

Here is the lines. I tried to put all on one function to the commands use the same variables, like on Wiki, but still don't working.

function chatFunctions (player, commandName, message)
local posX, posY, posZ = getElementPosition( player )
local chatSphere = createColSphere( posX, posY, posZ, chatRadius )
local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" )
local playerNameIs = getPlayerName (player)
if(commandName == "ooc" or commandName == "o") then
if(message) then
if(AllowOOC == 1) then
outputChatBox( "(( [GLOBAL OOC]"..playerNameIs..": "..message" ))" )
else
outputChatBox ( "O Chat OOC foi bloqueado por um administrador.", player, 255, 153, 0 )
end
else
outputChatBox ( "Uso Correto: /(o)oc [texto]", player, 255, 153, 0 )
end
elseif(commandName == "b") then
for index, nearbyPlayer in ipairs( nearbyPlayers ) do
if(message) then
outputChatBox( "(( [LOCAL OOC]"..playerNameIs..": "..message" ))", nearbyPlayer )
else
outputChatBox ( "Uso Correto: /b [texto]", player, 255, 153, 0 )
end
end
elseif(commandName == "do") then
for index, nearbyPlayer in ipairs( nearbyPlayers ) do
if(message) then
outputChatBox ( message.." (( "..playerNameIs.." ))", nearbyPlayer, 255, 153, 0 )
else
outputChatBox ( "Uso Correto: /do [ação]", player, 255, 153, 0 )
end
end
elseif(commandName == "me") then
for index, nearbyPlayer in ipairs( nearbyPlayers ) do
if(message) then
outputChatBox( playerNameIs.." "..message, nearbyPlayer, 153, 51, 153 )
else
outputChatBox ( "Uso Correto: /me [ação]", player, 255, 153, 0 )
end
end
end
end
addCommandHandler( "ooc", chatFunctions )
addCommandHandler( "o", chatFunctions )
addCommandHandler( "b", chatFunctions )
addCommandHandler( "do", chatFunctions )
addCommandHandler( "me", chatFunctions )

Thanks :)

Link to comment

your /(o)oc and /b commands are missing the '..' after message:

outputChatBox( "(( [GLOBAL OOC]"..playerNameIs..": "..message" ))" )

should be:

outputChatBox( "(( [GLOBAL OOC]"..playerNameIs..": "..message.." ))" )

you are right, /me should be done using onPlayerChat rather than adding your own command handler:

addEventHandler("onPlayerChat",root,
function(message,messageType)
if messageType == 1 then
cancelEvent()
 
-- output your message here
end
end
)

Link to comment

lol, I haven't noticed about the missing ".."

Now the command works :)

But I've put the follow lines to show the entire message

local words = {...}

local message = table.concat(words, " ")

but now he wasn't checking if exists some message, to return the 'Correct Usage' phrase...

What I need to do?

Link to comment

You can check the size of arg table. If it's 0 then the player hasn't entered any message. You don't have to make a new table of arguments because there is one already inside functions that use ....

if #arg == 0 then
outputChatBox( "Correct usage: /".. commandName .." [message]", player );
else
-- do whatever you want if there were arguments passed as message
outputChatBox( "(( [GLOBAL OOC]"..playerNameIs..": ".. table.concat( arg, " " ) .. " ))" );
end

Link to comment

Thanks 50p, very useful, now works :)

Last question, to "close" my questions...

I set up a PM system, but how can I do to return a full name only typing a part of the name?

Ex.: /pm sim Hi = "PM to simstosh: Hi"

I do here, but only with getPlayerFromName, so he needs the full name...

And how can I disable the GTA clock to use a fixed time?

I searched but haven't results...

Thanks

Link to comment

1.

You have to iterate through all players and check if their name starts with the name that player entered (I'll call this parameter partOfName)

for i, plr in ipairs( getElementsByType( "player" ) ) do
if plr ~= playerWhoTypedCommand then -- make sure that currently iterated player is not player who typed command
local start = string.find( getPlayerName( plr ), partOfName, 1, true );
-- check if partOfName was found in player name and if it was found at first character of the name
if start and start == 1 then 
-- player name starts with the partOfName
           outoutChatBox( "YOUR MESSAGE", plr );
end
end
end

2.

https://wiki.multitheftauto.com/wiki/SetMinuteDuration

https://wiki.multitheftauto.com/wiki/SetTime

Link to comment
You can check the size of arg table. If it's 0 then the player hasn't entered any message. You don't have to make a new table of arguments because there is one already inside functions that use ....
if #arg == 0 then
outputChatBox( "Correct usage: /".. commandName .." [message]", player );
else
-- do whatever you want if there were arguments passed as message
outputChatBox( "(( [GLOBAL OOC]"..playerNameIs..": ".. table.concat( arg, " " ) .. " ))" );
end

Note, that the arg table is deprecated in favour of {...} in Lua 5.1.2. It may be removed in future versions so it's not advisable to use it.

Link to comment

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