Help with invisible script/addon

Hello,

Well to be fast and simple I need help with making so when i type this command (!avanish) the player would turn invisible and to undo you have to type (!aunvanish)


/*------------------------------------------------------------------------------
                                Code
------------------------------------------------------------------------------*/

hook.Add( "OnPlayerChat", "PlayerSayExample", function(ply, text)
	if ( ply:GetUserGroup() == AVanished.GroupsAllowed and text:lower() == "!avanish" ) then

local ply = LocalPlayer()

  ply:ChatPrint("You have vanished.")
  return true

else

  ply:ChatPrint("You don't have permission to use this command.")
  return true

end
  end)

If there is anything wrong with the code please alert me. I am here to learn.

Besides that do I need to add anything else to work on a server?

You’re creating the ply variable INSIDE an if statement, and trying to access it in another section of the statement, in the else part. This won’t work due to how scopes work. Also, OnPlayerChat is a clientside hook. You’ll probably want to use PlayerSay



hook.Add( "PlayerSay", "Vanish", function( ply, text, team )

	if ( text:lower() == "!avanish" ) then

		if ( ply:GetUserGroup() == AVanished.GroupsAllowed ) then
			ply:SetRenderMode( RENDERMODE_TRANSALPHA ) // Based on some old code
			ply:SetColor( Color( 0, 0, 0, 0 ) ) // Based on some old code
			ply:ChatPrint("You have vanished.")
		else
			ply:ChatPrint("You don't have permission to use this command.")
		end

		return "" // Cancels the text "!avanish" from appearing in the chatbox
	end

end )


Tested, this works surprisingly

So to make they go back to normal I would put on the SetColor 0,0,0,255??

Reset it to 255, 255, 255, 255, and reset the render mode to opaque.

okay thx