General > Developers Corner

Chat Triggers

<< < (3/5) > >>

MrPresident:
The code "strong.lower" converts everything you type into lower case to make the string comparison more compatible. I made a mistake when I put "Hurr durr" in my example.


for example:

local test = "ThIs Is All KiNdS of CaPitAlIsM"

test = string.lower(test)

print(test)

the result would be "this is all kinds of capitalism"


The idea is that it would recognize whenever a player typed "hurr durr" no matter how they capitalize it.

I fixed my example code.

Decicus:
Now it worked completely fine with both tsay and tsayColor. Now one last question, just to be sure. I looked up "string.sub", while I was reading about it, I didn't find the information I wanted (there's a possibility that I just misunderstood it), but I'd like to know if you have to specify when the exact StartPos/EndPos is. What if I want it to recognize if "hurr durr" was said somewhere in the chat? I mean: If "hurr durr" was included in a message, and that would still trigger "I LIKE TO HERP DERP". Is this possible? I haven't found anything about it yet, although, as I said I might have missed it.

Thanks for all the help so far! It's all appreciated, this question is probably my last one, after that I think I'll be able to piece my own code together.

MrPresident:
string.sub does require at least a start point and returns a string derivative of the string you pass it.

To do what you want, take a look at string.find

An example of this would be...


--- Code: ---function HurDerp( ply, text, _ )

if ( string.find( string.lower(text), "hurr durr" ) then
ULib.tsay( ply, "I LIKE TO HERP DERP")
end

end
hook.Add( "PlayerSay", "HurDerp_Hook", HurDerp )

--- End code ---

string.find returns the start and stop position of a string within another string. But simple checking it as a boolean will work because it will return nil if it doesn't find the string within the target string.

Please ask any questions you have. We (I especially) enjoy helping other people learn lua.

Decicus:
Ah, thank you, it makes sense to me now.

I said that was probably my last question, sort of lied, because now I have another question. I have a script that triggers when someone joins (it's sort of not connected to this, but it sort of is in a way).

So whenever a specific person joins, it triggers a playsound that works fine, but it should also print a message. The issue I'm having, is that I want it to grab the person's username (the one that's joining) while using ULib.tsayColor to print a colored message. I've tried adding ply:Nick() into it, but it doesn't work. Is there a way I can do it without having to manually add the username to each player in the group, and let it grab the username from the one they're currently using?

JamminR:
Sure that ply is the variable in your code for the player object?
<player_object>:Name() may work for you too.
Really difficult to know what's wrong without a code example.
Can depend on where the code is running too (server side or client side)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version