Text Code Help

I on my deathrun I want to modify the chat so when someone says like “cp” its plays the sound “CPSSSS” like in darkrp.

Im not sure when I would put it, im guessing lua/autorun/server. I am trying to learn lua so I want to know how it works and why (if you’re willing to help).

Im guess it would be something like:


if text == "cp" then {
    Play.Sound(sound file)
end
}

I really don’t know lua, just a bit of JS.

Lua doesn’t use brackets.


if text == "cp" then
    Play.Sound(sound file)
end


Oh, thanks.

[editline]1st June 2014[/editline]

After looking on the wiki, I found this:


sound.PlayFile( string path, string flags, function callback ) 

So im guess it will be:


if text == "CP" then
   sound.PlayFile( sound/music/vlvx_song22.mp3, "", "" )
end


-- Run clientside

function cps ( ply, text, teamchat, isDead )
   if ( IsValid( ply ) and string.lower( text ) == "cp" and not isDead ) then
       surface.PlaySound( "file" ) -- Will only play to the client. If you want others around to hear, use sound.Play
   end
end

hook.Add( "OnPlayerChat", "CPsss", cps )

Thank you very much! :slight_smile: But can you explain:


 if ( IsValid( ply ) and string.lower( text ) == "cp" and not isDead )

  1. Checks if the player who is saying it isn’t nil (precaution)
  2. The player can type CP, Cp, cP, or cp and they will all work because string.lower converts all letters in a string to lowercase.
  3. Makes sure the player is alive (you can change that if you want, personal preference)

What’s to explain? IsValid checks if the player is a valid entity (which they should be), string.lower converts the string to lower-case, so if they type CP or cP it will still work, and ‘not isDead’ checks if they are alive.

Thanks mate!