I wrote a simple script that checks the chat for words from the table. In this case, to receive health.
But...
[CODE]
[ERROR] lua/autorun/checkchat.lua:13: attempt to index local 'ply' (a nil value)
1. func - lua/autorun/checkchat.lua:13
2. AddPhrases - lua/autorun/checkchat.lua:3
3. v - lua/autorun/checkchat.lua:20
4. unknown - lua/includes/modules/hook.lua:84
[/CODE]
Any ideas how to fix this?
Here is the script:
[CODE]
function AddPhrases(args, phrases, func)
if table.concat(args, " ", 1, #args) == table.concat(phrases, " ", 1, #phrases) then
func()
end
end
HealthPhrases = {"give", "health"}
function GiveHealth(ply)
local hp = math.random(20, 50)
ply:SetHealth(ply:Health() + hp)
ply:ChatPrint("Here are your ".. hp .." HP, ".. ply:Name() ..".")
end
hook.Add("PlayerSay", "ChatCheck", function(ply, said)
args = string.Split(string.lower(said), " ")
AddPhrases(args, HealthPhrases, GiveHealth)
end)
[/CODE]
is function AddPhrases(args, phrases, func) line 1?
I changed your script a bit to something less confusing:
[CODE]
local phrases = {}
local function AddPhrase( str, func )
if !isstring( str ) then return end
if !isfunction( func ) then return end
phrases[ string.lower( str ) ] = func
end
hook.Add( "PlayerSay", "ChatCheck", function( ply, text )
local func = phrases[ string.lower( text ) ]
if isfunction( func ) then
func( ply )
return ""
end
end )
AddPhrase( "give health", function( ply )
local hp = math.random( 20, 50 )
ply:SetHealth( ply:Health() + hp )
ply:ChatPrint( "Here's " .. hp .. " HP, " .. ply:Name() .. "." )
end )
[/CODE]
This should work if you type 'give health' in the chat.
[editline]3rd July 2017[/editline]
[URL="https://facepunch.com/showthread.php?t=1209255&p=37490686&viewfull=1#post37490686"]Actually, this one is better[/URL]
Sorry, you need to Log In to post a reply to this thread.