General > Developers Corner
Admin sounds, tools and other code chat.
jay209015:
This is on a different subject, but I don't feel it's necessary to open a new topic.
--- Code: ---WordD = { "", "ass", "" }
function WFilter( ply, text, toall )
for _, v in pairs( WordD ) do
if string.find( text, v) then
WordC = string.Replace(text,V,"****")
return ..WordC..
else
WordC = (text)
return ..WordC..
end
end
end
hook.Add( "PlayerSay", "WFilter", WFilter );
--- End code ---
I want this to replace the words in WordD when found in chat with **** , but I can't figure out how. Any help?
JamminR:
'return' will always end a loop.
Though your code _may_ find one word, it will stop looping.
Define a local variable before your for loop, such as local WordC = ""
than after the end of the for loop, -then- return WordC, and remove the other returns.
There is no need to use ".." before and after your WordC.
V does not equal v in lua.
Lua is case sensitive. You have the wrong case for one of you're variables.
jay209015:
Ok, so is this what you're saying?
--- Code: ---WordD = { "", "ass", "" }
local WordC = ""
function WFilter( ply, text, toall )
for _, v in pairs( WordD ) do
if string.find( text, v) then
WordC = string.Replace(text,v,"****")
else
WordC = (text)
end
end
return WordC
end
hook.Add( "PlayerSay", "WFilter", WFilter );
--- End code ---
BTW, thanks for the move, and all the help you've given me so far.
JamminR:
Yes, that should work.
To help make table loops faster, use ipairs instead of pairs when possible.
An experienced coder once taught me it was more efficient for the purpose your using it.
If you had a complex non-sequential numbered table, then you'd need pairs. Complex non-indexed example blah = { something = "else", old = "notnew" )
Since you're using a simple table where WordD.1 = someword and WordD.2 = someotherword and WordD.3 = yetsomeotherword, you can use ipairs
i meaning indexed, 1, 2, 3...
jay209015:
But, do you know why this script isn't working at all?
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version